[英]How to use different filters in geom_errorbar of ggplot2 in R
使用此示例數據框:
> dput(coun2b)
structure(list(Camden = c(13.9933481152993, 17.5410199556541,
26.0055432372506, 19.1064301552106, 9.05764966740577, 17.5321507760532
), Guilford = c(24.674715261959, 27.5097949886105, 25.4646924829157,
22.2637813211845, 7.60227790432802, 17.9681093394077), years = 2012:2017,
Camden_ymin = c(12.4514939737261, 15.4927722105436, 22.5744436662436,
16.8415649174844, 7.45264839077184, 15.6645677387521), Guilford_ymin = c(23.2136204848819,
26.3627764588421, 23.8076842636931, 20.383805927254, 5.58799564906578,
16.2548749333076), Camden_ymax = c(15.5352022568726, 19.5892677007646,
29.4366428082575, 21.3712953929369, 10.6626509440397, 19.3997338133543
), Guilford_ymax = c(26.1358100390361, 28.6568135183788,
27.1217007021384, 24.143756715115, 9.61656015959026, 19.6813437455079
)), class = "data.frame", row.names = c(NA, -6L))
看起來像這樣:
coun2b
Camden Guilford Camden_ymin Guilford_ymin Camden_ymax Guilford_ymax
1 13.99335 24.674715 12.451494 23.213620 15.53520 26.13581
2 17.54102 27.509795 15.492772 26.362776 19.58927 28.65681
3 26.00554 25.464692 22.574444 23.807684 29.43664 27.12170
4 19.10643 22.263781 16.841565 20.383806 21.37130 24.14376
5 9.05765 7.602278 7.452648 5.587996 10.66265 9.61656
6 17.53215 17.968109 15.664568 16.254875 19.39973 19.68134
我用這個 dataframe:
library(tidyverse)
ggplot(coun2b, aes(x=years, Guilford, group=years)) +
labs(title = "Counts in Guilford, N.C.",
#caption="P. infestans range: 18 - 22 C; P. nicotianae range: 25 - 35 C; \"a\" Year with\nmost N.C. P. infestans reports (n=16); \"aa\" Year with most N.C. P. nicotianae reports (n=23)",
y="Number of Days", x="Year" ) + geom_col( position = "dodge") +
geom_errorbar(aes(ymin=Guilford_ymin, ymax=Guilford_ymax), position="dodge") +
theme(axis.text.x = element_text(face="bold"), axis.title.x = element_text(size=14),
axis.text.y = element_text(face="bold"), axis.title.y = element_text(size=14),
title = element_text(size=12)) +
scale_x_continuous("Year", labels = plotscalex, breaks=plotscalex) +
geom_hline(aes(yintercept = mean(Guilford[years %in% 2012:2016]),
linetype='Mean for 2012-2016')) +
scale_linetype_manual(name="Legend", values=c("Mean for 2012-2016"=1) )
我創建了這個條形圖:
然而,我的完整數據集實際上更大,形狀也不同,就像長版本一樣。 這是長版本的示例:
> dput(samp1)
structure(list(years = c(2012L, 2012L, 2012L, 2013L, 2013L, 2013L,
2014L, 2014L, 2014L, 2012L, 2012L, 2012L, 2013L, 2013L, 2013L,
2014L, 2014L, 2014L), valu = c("mean", "ymin", "ymax", "mean",
"ymin", "ymax", "mean", "ymin", "ymax", "mean", "ymin", "ymax",
"mean", "ymin", "ymax", "mean", "ymin", "ymax"), name = c("Camden",
"Camden", "Camden", "Camden", "Camden", "Camden", "Camden", "Camden",
"Camden", "Guilford", "Guilford", "Guilford", "Guilford", "Guilford",
"Guilford", "Guilford", "Guilford", "Guilford"), value = c(13.9933481152993,
12.4514939737261, 15.5352022568726, 17.5410199556541, 15.4927722105436,
19.5892677007646, 26.0055432372506, 22.5744436662436, 29.4366428082575,
24.674715261959, 23.2136204848819, 26.1358100390361, 27.5097949886105,
26.3627764588421, 28.6568135183788, 25.4646924829157, 23.8076842636931,
27.1217007021384), county = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("Camden",
"Guilford", "Pasquotank", "Wake"), class = "factor")), row.names = c(NA,
-18L), class = c("tbl_df", "tbl", "data.frame"))
我嘗試使用:
samp1 %>% filter(county == "Camden") %>%
ggplot( aes(x=years, y=value, group=years)) +
labs(title = "Number of Days in April-August with Suitable Weather for\nLate Blight Sporulation in Camden, N.C.", y="Number of Days", x="Year" ) +
geom_col(data=samp1 %>% filter(county=="Camden", valu=="mean"), aes(x=years,
y=value), position = "dodge") +
geom_errorbar(data=samp1 %>% filter(county=="Camden"),
aes(ymin=samp1 %>% filter(valu=="ymin"), ymax=samp1 %>% filter(valu=="ymax"), position="dodge")) +
theme(axis.text.x = element_text(face="bold"), axis.title.x = element_text(size=14),
axis.text.y = element_text(face="bold"), axis.title.y = element_text(size=14),
title = element_text(size=12)) +
scale_x_continuous("Year", labels = plotscalex, breaks=plotscalex) +
geom_hline(aes(yintercept = mean(Camden[years %in% 2012:2016]),
linetype='Mean for 2012-2016'))+
scale_linetype_manual(name="Legend", values=c("Mean for 2012-2016"=1) )
嘗試使用長格式的數據創建與上面相同的 plot。 我收到此錯誤消息:
Error in `geom_errorbar()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 2nd layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (9)
✖ Fix the following mappings: `ymin` and `ymax`
Run `rlang::last_error()` to see where the error occurred.
Warning message:
In geom_errorbar(data = samp1 %>% filter(county == "Camden"), aes(ymin = samp1 %>% :
Ignoring unknown aesthetics: position
由於這個 dataframe 的長格式,我在到達geom_errorbar()
之前使用了filter
2x。 我不認為這是問題所在,我只是不知道如何正確filter
ymin 和 ymax。 我試過geom_errorbar(data=samp1 %>% filter(county=="Camden"), aes(ymin=samp1 %>% filter(county=="Camden", valu=="ymin"), ymax=samp1 %>% filter(county=="Camden",valu=="ymax"), position="dodge"))
以及上面代碼塊中的內容,我無法讓它工作。 我如何使用長格式數據samp1
創建一個 plot ,它與寬數據時創建的 plot 相同? 我使用長表格是因為我必須為多個縣繪制並排條形圖,而在這篇文章中,我只使用一個縣。
您正在使這比需要的困難得多。 一個簡單的 pivot 首先將您的數據轉換為正確的格式有什么問題? 然后,您在 plot 代碼中唯一需要解決的問題是獲取hline
:
library(tidyverse)
sampl %>%
pivot_wider(names_from = valu, values_from = value) %>%
ggplot(aes(years, mean)) +
geom_col() +
geom_errorbar(aes(ymin = ymin, ymax = ymax), width = 0.25) +
geom_hline(data = . %>% group_by(county) %>% summarize(mean = mean(mean)),
aes(yintercept = mean), linetype = 2) +
facet_grid(.~county) +
theme_gray(base_size = 16) +
theme(strip.background = element_blank(),
strip.text = element_text(size = 20, face = 2))
或者,如果您想一次執行一個 plot:
sampl %>%
pivot_wider(names_from = valu, values_from = value) %>%
filter(county == "Guilford") %>%
ggplot(aes(years, mean)) +
geom_col() +
geom_errorbar(aes(ymin = ymin, ymax = ymax), width = 0.25) +
geom_hline(aes(yintercept = mean(mean)), linetype = 2) +
theme_gray(base_size = 16) +
ggtitle("Guilford")
您收到錯誤是因為第二個數據框的格式不正確:通過旋轉我們可以將 ymin 和 ymax 設置為列: 然后我們可以只過濾一次並應用代碼:
library(tidyverse)
samp1 %>%
pivot_wider(names_from = valu,
values_from = value) %>%
filter(county == "Camden") %>%
ggplot( aes(x=years, y=mean, group=years)) +
labs(title = "Number of Days in April-August with Suitable Weather for\nLate Blight Sporulation in Camden, N.C.", y="Number of Days", x="Year" ) +
geom_col(position = "dodge")+
geom_errorbar(aes(ymin=ymin, ymax=ymax), position="dodge")+
theme(axis.text.x = element_text(face="bold"), axis.title.x = element_text(size=14),
axis.text.y = element_text(face="bold"), axis.title.y = element_text(size=14),
title = element_text(size=12)) +
geom_hline(aes(yintercept = mean(mean),
linetype='Mean for 2012-2016'))+
scale_linetype_manual(name="Legend", values=c("Mean for 2012-2016"=1) )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.