从第 1 年和第 2 年的两个并排条形图创建组合的单个条形图或直方图
Creating a combined single barplot or histogram from two different charts with bars side by side for years 1 & 2
我有很多树木的疾病评分,其中有连续两年的枯死率/感染率读数。我可以为每年在 r
中的 ggplot2
中创建一个直方图,但是我如何创建一个并排的条形图来显示每年的读数与范围并排,例如x 轴上 0-10、10-20、20-30% 直至 100% 感染?
我创建了一个简单的数据框,每年有两个读数:
diseaseyear1 <- c(20, 30, 40, 50, 30, 20, 60, 85, 45, 32, 20, 40)
diseaseyear2 <- c(30, 35, 42, 45, 25, 70, 65, 90, 40, 25, 35, 50)
totaldisease <- tibble(diseaseyear1, diseaseyear2)
totaldisease
#I can plot year 1
quartz(10, 5)
year1 <- ggplot(totaldisease) +
aes(x=diseaseyear1) +
geom_histogram(binwidth = 10) +
labs(title = "Disease Year 1",
y = "count", x = "% of disease") +
theme(text = element_text(size = 10, family = "GillSans"))
#I can also plot year 2
year1 <- ggplot(totaldisease) +
aes(x=diseaseyear1) +
geom_histogram(binwidth = 10) +
labs(title = "Disease Year 1 & 2",
y = "count", x = "% of disease") +
theme(text = element_text(size = 10, family = "GillSans"))
#but how can I combine these two histograms (or bar plots) as side by side bars in one chart?
我们需要先将数据转换为长格式。这样,我们可以更好地绘制数据。
totaldisease %>%
tidyr::pivot_longer(names_to = "dis", values_to = "val",
cols = 1:ncol(.)) %>%
ggplot(aes(val, fill=dis))+
geom_histogram(binwidth = 10)+
labs(title = "Disease Year 1 & 2",
y = "count", x = "% of disease") +
theme(text = element_text(size = 10, family = "GillSans"))
结果
需要就闪避
totaldisease %>%
tidyr::pivot_longer(names_to = "dis", values_to = "val",
cols = 1:ncol(.)) %>%
ggplot(aes(val, fill=dis))+
geom_histogram(binwidth = 10, position = "dodge")+
labs(title = "Disease Year 1 & 2",
y = "count", x = "% of disease") +
theme(text = element_text(size = 10, family = "GillSans"))
我有很多树木的疾病评分,其中有连续两年的枯死率/感染率读数。我可以为每年在 r
中的 ggplot2
中创建一个直方图,但是我如何创建一个并排的条形图来显示每年的读数与范围并排,例如x 轴上 0-10、10-20、20-30% 直至 100% 感染?
我创建了一个简单的数据框,每年有两个读数:
diseaseyear1 <- c(20, 30, 40, 50, 30, 20, 60, 85, 45, 32, 20, 40)
diseaseyear2 <- c(30, 35, 42, 45, 25, 70, 65, 90, 40, 25, 35, 50)
totaldisease <- tibble(diseaseyear1, diseaseyear2)
totaldisease
#I can plot year 1
quartz(10, 5)
year1 <- ggplot(totaldisease) +
aes(x=diseaseyear1) +
geom_histogram(binwidth = 10) +
labs(title = "Disease Year 1",
y = "count", x = "% of disease") +
theme(text = element_text(size = 10, family = "GillSans"))
#I can also plot year 2
year1 <- ggplot(totaldisease) +
aes(x=diseaseyear1) +
geom_histogram(binwidth = 10) +
labs(title = "Disease Year 1 & 2",
y = "count", x = "% of disease") +
theme(text = element_text(size = 10, family = "GillSans"))
#but how can I combine these two histograms (or bar plots) as side by side bars in one chart?
我们需要先将数据转换为长格式。这样,我们可以更好地绘制数据。
totaldisease %>%
tidyr::pivot_longer(names_to = "dis", values_to = "val",
cols = 1:ncol(.)) %>%
ggplot(aes(val, fill=dis))+
geom_histogram(binwidth = 10)+
labs(title = "Disease Year 1 & 2",
y = "count", x = "% of disease") +
theme(text = element_text(size = 10, family = "GillSans"))
结果
需要就闪避
totaldisease %>%
tidyr::pivot_longer(names_to = "dis", values_to = "val",
cols = 1:ncol(.)) %>%
ggplot(aes(val, fill=dis))+
geom_histogram(binwidth = 10, position = "dodge")+
labs(title = "Disease Year 1 & 2",
y = "count", x = "% of disease") +
theme(text = element_text(size = 10, family = "GillSans"))