R - ggplot2 geom_line躲闪

时间:2016-10-18 15:10:17

标签: r ggplot2

我在绘制" dodges" line on" dodged" stacked bars

dt = mtcars %>% group_by(am, cyl) %>% summarise(m = mean(disp))
dt0 = dt[dt$am == 0, ]
dt1 = dt[dt$am == 1, ]

dt0 %>% ggplot(aes(factor(cyl), m, fill = factor(cyl))) + geom_bar(stat = 'identity', position = 'dodge') +
  geom_point(data = dt1, aes(factor(cyl), m, colour = factor(cyl)), position=position_dodge(width=0.9), colour = 'black') 

enter image description here

我想要的是从堆积条的顶部到每个cyl的黑色画一条线。

dt0 %>% ggplot(aes(factor(cyl), m, fill = factor(cyl))) + geom_bar(stat = 'identity', position = 'dodge') +
  geom_point(data = dt1, aes(factor(cyl), m, colour = factor(cyl)), position=position_dodge(width=0.9), colour = 'black') + 
  geom_line(data = dt1, aes(factor(cyl), m, colour = factor(cyl), group = 1), position=position_dodge(width=0.9), colour = 'black')  

然而,position=position_dodge(width=0.9)闪避在这里不起作用。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如果您重塑摘要数据,这将更容易实现:

g = sns.PairGrid(hyperparams, diag_sharey=False)
g.map_lower(sns.kdeplot)
g.map_upper(plt.scatter, marker='+')
g.map_diag(sns.kdeplot)

而&#34; 0&#34;和&#34; 1&#34;如果你用反引号引用它们,它们仍然可以在dt <- mtcars %>% group_by(am, cyl) %>% summarise(m = mean(disp)) %>% spread(am, m) cyl 0 1 * <dbl> <dbl> <dbl> 1 4 135.8667 93.6125 2 6 204.5500 155.0000 3 8 357.6167 326.0000 中使用。拨打aes()也变得不必要了:

position_dodge()

enter image description here