1

I have this data frame

df <- data.frame(profile = rep(c(1,2), times = 1, each = 3), depth = c(100, 200, 300), value = 1:3)

This is my plot

ggplot() + 
  geom_bar(data = df, aes(x = profile, y = - depth, fill = value), stat = "identity")

My problem is the y labels which doesn't correspond to the depth values of the data frame

To help, my desired plot seems like this :

ggplot() + geom_point(data = df, aes(x = profile, y = depth, colour = value), size = 20) + xlim(c(0,3))

But with bar intead of points vertically aligned

nb : I don't want to correct it manually in changing ticks with scale_y_discrete(labels = (desired_labels))

Thanks for help

Loulou
  • 703
  • 5
  • 17
  • 1
    Why are you using `max` in y-axis? – amrrs Feb 14 '18 at 11:42
  • Correct ! It is not necessary. I have edited the post – Loulou Feb 14 '18 at 11:45
  • What's your expected output bar? – amrrs Feb 14 '18 at 11:45
  • My expected output bar is a bar with ticks from 0 to -300 – Loulou Feb 14 '18 at 11:47
  • How's that possible, when you group you data with profile, 1 will have value 600? that brings to the question what you want in your x-axis? – amrrs Feb 14 '18 at 11:49
  • I edited my post in adding a plot which is similar to my wished plot – Loulou Feb 14 '18 at 11:55
  • `ggplot(df, aes(x = factor(profile), y = -depth, fill = value, group = interaction(profile, value))) + geom_col(position = 'dodge')`? – Axeman Feb 14 '18 at 13:13
  • It's the way I wish. The last step is to stack the columns without summing up the depth – Loulou Feb 14 '18 at 13:21
  • You can overplot, but it is tricky to set the `group` correctly (in order to plot the smallest bar on top). `ggplot(df, aes(x = factor(profile), y = -depth, fill = value, group = factor(interaction(profile, value), rev(interaction(profile, value))))) + geom_col()`. Note that this plot is ambiguous as to what the bars mean (true values or sums). – Axeman Feb 14 '18 at 13:42

2 Answers2

0

Considering you want a y-axis from 0 to -300, using facet_grid() seems to be a right option without summarising the data together.

ggplot() + geom_bar(data = df, aes(x = as.factor(profile), y = -depth, fill = value), stat = 'identity') + facet_grid(~ value)

enter image description here

amrrs
  • 6,215
  • 2
  • 18
  • 27
  • To help, my desired plot seems like this : ggplot() + geom_point(data = df, aes(x = profile, y = depth, colour = value), size = 20) + xlim(c(0,3)) But with bar intead of points vertically aligned – Loulou Feb 14 '18 at 11:57
  • But remember then it'll have value upto 600 because of the total sum. You can one like this or side by side bars. – amrrs Feb 14 '18 at 11:58
0

I have it !

Thanks for your replies and to this post R, subtract value from previous row, group by

To resume; the data :

df <- data.frame(profile = rep(c(1,2), times = 1, each = 3), depth = c(100, 200, 300), value = 1:3)

Then we compute the depth step of each profile :

df$diff <- ave(df$depth, df$profile, FUN=function(z) c(z[1], diff(z)))

And finally the plot :

ggplot(df, aes(x = factor(profile), y = -diff, fill = value)) + geom_col()
Loulou
  • 703
  • 5
  • 17