首页 > 解决方案 > 使用 ggplot R 将条形图和折线图与数值和分类变量相结合

问题描述

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

在此处输入图像描述

到目前为止,我有这个代码:

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

在此处输入图像描述

标签: rggplot2bar-chartcategorical-datacontinuous

解决方案


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根据需要更改

在此处输入图像描述


推荐阅读