我希望这个标题不要太混乱。我有10个日期的数据,每个数据点都标为“灌溉”或“降水”。我想要制作一个条形图,它有一个按时间顺序排列的条形图,标有标有日期的条形图,但也希望条形图的颜色被编码(例如,“灌溉”中的所有条形图都是红色的,而来自“沉淀”的条形图则是蓝色的)。
我对R相当陌生,这有可能吗?如果是这样的话,任何帮助都将不胜感激。
这是我的数据:
dput(head(data))
structure(list(Date = structure(4:9, .Label = c("7/11/2019",
"7/13/2019", "7/15/2019", "7/2/2019", "7/3/2019", "7/4/2019",
"7/5/2019", "7/6/2019", "7/7/2019", "7/9/2019"), class = "factor"),
Inches = c(1.6, 0.02, 0.35, 0.64, 0.07, 0.35), Type = structure(c(2L,
2L, 1L, 2L, 2L, 1L), .Label = c("Irrigation", "Precipitation"
), class = "factor")), row.names = c(NA, 6L), class = "data.frame")发布于 2019-07-16 16:13:05
您可以使用dplyr和ggplot2来生成条形图。stat='identity'调用意味着每个条形图高度都使用数据中的对应值,而position='dodge'则将条形图并排放置,而不是堆叠。
df <- data.frame(date=c(paste0('2019-0',1:9),'2019-10'),
precipitation=runif(10),irrigation=runif(10))
df
df %>% gather(k,v,-date) %>%
ggplot(aes(date,v,fill=k)) + geom_bar(stat='identity',position = 'dodge') +
theme(axis.text.x = element_text(angle = 45,hjust=1))

编辑
下面是使用您从dput提供的数据的条形图。我不知道你的数据已经被分组在一列里了。
df <- structure(list(Date = structure(4:9, .Label = c("7/11/2019",
"7/13/2019", "7/15/2019", "7/2/2019", "7/3/2019", "7/4/2019",
"7/5/2019", "7/6/2019", "7/7/2019", "7/9/2019"), class = "factor"),
Inches = c(1.6, 0.02, 0.35, 0.64, 0.07, 0.35), Type = structure(c(2L,
2L, 1L, 2L, 2L, 1L), .Label = c("Irrigation", "Precipitation"
), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
df %>% ggplot(aes(Date,Inches,fill=Type)) + geom_bar(stat='identity',position = 'dodge') +
theme(axis.text.x = element_text(angle = 45,hjust=1))

https://stackoverflow.com/questions/57061354
复制相似问题