我已经创建了以下的 ggplot 图形
使用以下代码:
我试图有意地重叠这些条形图。我想知道如何更改每个条形图的 z-index(深度)。我尝试通过重新排序决定我的条形图的因子列的级别来实现这一点。
有人知道ggplot2是否能做到这一点吗?
编辑:
数据框的结构类似于以下:
![enter image description here](https://istack.dev59.com/tkHUF.webp)
p <- ggplot(df, aes(x=100*prop_select, y=(1500-s_meanResponse), fill=product)) +
geom_bar(stat='identity', width=2, alpha=.6, color='#333333') +
coord_cartesian(xlim = c(0, 5)) +
coord_flip()
print(p)
我试图有意地重叠这些条形图。我想知道如何更改每个条形图的 z-index(深度)。我尝试通过重新排序决定我的条形图的因子列的级别来实现这一点。
# Order by mean response to make sure that bars are layered correctly (does not work!)
# Switching this minus makes no difference to the bar z-index but does reverse legend entries
df <- df[sort.list(-df$s_meanResponse),]
df$product <- factor(df$product, levels=df$product)
有人知道ggplot2是否能做到这一点吗?
编辑:
数据框的结构类似于以下:
df <- data.frame( product=c('a','b','c'), s_meanResponse=c(1120,1421,1320), prop_select=c(.3,.2,.5))
df
的样本。 - Matthew Plourdefactor()
表达式中使用levels=rev(df$product)
。 - James Trimble