R条形图数据行
我试图在R中绘制一个包含以下数据的图表:R条形图数据行,r,bar-chart,R,Bar Chart,我试图在R中绘制一个包含以下数据的图表: sex ethnicity x2015 x2016 x2017 ------------------------------- male dutch 112 117 116 female dutch 114 118 120 female german 102 101 99 etc 我想从每个数据行制作一个bart图表 谢谢你的帮助 您可以使用stat=identity
sex ethnicity x2015 x2016 x2017
-------------------------------
male dutch 112 117 116
female dutch 114 118 120
female german 102 101 99
etc
我想从每个数据行制作一个bart图表
谢谢你的帮助 您可以使用stat=identity和facet\u wrap使用ggplot2包。此外,您还可以使用Reforme2包中的melt函数将数据转换为便于在ggplot2中使用的数据集格式(从宽数据集格式转换为窄数据集格式)。请参阅下面的代码:
library(ggplot2)
library(reshape2)
data <- structure(list(sex = structure(c(2L, 1L, 1L), .Label = c("female",
"male"), class = "factor"), ethnicity = structure(c(1L, 1L, 2L
), .Label = c("dutch", "german"), class = "factor"), x2015 = c(112L,
114L, 102L), x2016 = c(117L, 118L, 101L), x2017 = c(116L, 120L,
99L)), class = "data.frame", row.names = c(NA, -3L))
df <- melt(data)
ggplot(df, aes(x = ethnicity, y = value, fill = variable)) +
geom_bar(stat = "identity") +
facet_wrap(~ sex)
输出:
欢迎使用stackoverflow,前面提到的不是代码编写服务,请先告诉我们您尝试了什么。见:可能重复的