将条形图和折线图与带有ggplot R的数值和分类变量结合

时间:2019-02-20 22:34:17

标签: r ggplot2 bar-chart categorical-data continuous

我正在尝试使用连续变量和分类变量在ggplot中组合条形图和折线图。我基本上需要做这样的事情:

enter image description here

到目前为止,我已经有了以下代码:

ggplot(data=nutrients, aes(x=Plot, y=Level, fill=Factor)) +
         geom_bar(stat="identity", position = 'dodge', colour="black")

enter image description here

1 个答案:

答案 0 :(得分:1)

ggplot(mtcars, aes(x = factor(cyl), y = wt, fill = factor(gear))) + 
    geom_bar(stat = "identity", position = "dodge") +
    geom_line(aes(y = ave(wt, cyl, FUN = max), group = gear)) + 
    geom_point(aes(y = ave(wt, cyl, FUN = max), group = gear))

根据需要更改FUN

enter image description here