使用此示例数据框:
> 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) )
尝试使用长格式的数据创建与上面相同的图。我收到以下错误消息:
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
,创建一个与数据较宽时创建的图相同的图?我使用长格式,因为我必须为多个县做一个并排的条形图,而在这篇文章中,我只使用一个县。
2条答案
按热度按时间luaexgnf1#
你让这变得比实际需要的要困难得多。一个简单的透视从一开始就把你的数据转换成正确的格式有什么问题吗?在绘图代码中你需要做的唯一的争论就是得到分组的
hline
:或者,如果要一次执行一个打印:
dba5bblo2#
出现错误是因为第二个 Dataframe 的格式不正确:通过旋转,我们可以将ymin和ymax设置为列:然后我们可以只过滤一次,并应用代码: