[英]Creating a bar chart with ggplot2 with dummy variables on x-axis as well as providing the y-values for the barplot
我的数据集有两个变量来描述我的观察结果:
我现在的目标是创建一个条形图,描述 1978 年治疗组和对照组的平均收入。 我现在的代码: chart1 = ggplot(data = NSW, aes(x = treatment, y = income)) + geom_bar(stat='identity')
我得到的是:在此处输入图像描述
我将代码更改为: chart1 = ggplot(data = NSW, aes(x = treatment, y = mean(income))) + geom_bar(stat='identity')
产生此条形图:在此处输入图像描述
我只是对如何告诉 R 我希望两个条形都在 y 轴上“停止”感到困惑,其中描述了任一组的各自平均收入。
编辑:这就是最终结果的样子(对不起,我画得不好):在此处输入图像描述图表的标题将类似于:1978 年的平均收入——治疗组和对照组
希望任何人都可以帮助我。 谢谢!
PS:不要混淆,在两个条形图中,收入的变量名称都标记为“re78”。
这是你想要的吗?
# Load libraries
library(ggplot)
# Create some fake data
income <- runif(n = 10, min = 0, max = 30000)
cohort <- factor(sample(x = 0:1, size = 10, replace = TRUE),
labels = c("control", "treatment"))
NSW <- data.frame(income, cohort)
# Plot the data
chart1 = ggplot(data = NSW, aes(x = cohort, y = income)) +
geom_bar(stat = "summary", fun = "mean") +
scale_y_continuous(labels=scales::dollar_format(),
expand = c(0, 0), name = "Mean Income")
chart1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.