简体   繁体   中英

R - geom_bar - 'stack' position without summing the values

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

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)

在此处输入图片说明

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM