I have a simple two variable data frame with a third variable acting as a factor
DF <- data.frame(Depth = c(8.6, 19.6, 42.6, 60.6, 79.4, 101.4, 121.4, 137.6, 163, 180),
Rb = c(103, 59, 99, 53, 107, 87, 52, 33, 105, 49),
Litho = as.factor(c(1,2,1,2,1,1,2,2,1,2)))
I want to create a bar graph of the absolute values so I am using geom_col()
. I want to plot Rb as a function of Depth therefore Depth should be the discrete variable. However, when I plot using
ggplot (DF, aes(x=Depth, y=Rb))+
geom_col()
the graph has horizontal bars that show how much Depth there is at each discrete Rb reading. I want to see the value of Rb at each discrete Depth.
Reversing the x and y gives the same problem, just with vertical bars
ggplot (DF, aes(x=Rb, y=Depth))+
geom_col()
I have also tried the same with geom_bar(stat = 'identity')
, but it's still the same problem.
EDIT - THIS WORKS IF ANYONE CAN EXPLAIN WHY
ggplot (DF, aes(x=Depth, y=Rb/10, fill=Litho)) +
geom_bar(stat='identity') +
labs(x="Depth", y="Rb") +
scale_x_continuous (trans = "reverse") +
scale_y_continuous (position = "right") +
coord_flip()
For some reason, dividing the Rb values by 10 sorts the problem out?? Dividing by any number greater than 2 works but if you divide by 1 or 2 (Rb, Rb/1, or Rb/2) it groups the data like in the above graphs and the bars are vertical, not horizontal??
Thanks,
Jeremy
See Question&Answers more detail:
os