新手在这里:) 我将感激你能给我的任何帮助建议。我正试图绘制scatterplotboxplothist的数据,我有一些视觉检查,让我们说我得到了我想要的地方,与一些其他命令......但后来当我尝试同样的ggplot,我不能得到它的结束。
这是我的数据 "alc3">的一部分,每个饮料类型都有虚拟变量。
Author est se beer wine spirits
1 Andrikopoulos and Loizides(2000) -1.00 0.18 1 0 0
2 Andrikopoulos and Loizides(2000) -0.35 0.32 1 0 0
3 Andrikopoulos et al. 1997 -1.00 0.46 1 0 0
4 Andrikopoulos et al. 1997 -1.02 0.46 1 0 0
5 Adrian and Ferguson(1987) -0.84 0.17 1 0 0
6 Andrikopoulos et al. 1997 -0.48 0.13 1 0 0
7 Andrikopoulos et al. 1997 -0.08 0.07 1 0 0
8 Quek(1988) -0.28 0.03 1 0 0
9 Johnson et al.(1992) -0.14 0.05 1 0 0
10 Johnson et al.(1992) -0.26 0.06 1 0 0
11 Selvanathan and Selvanathan(2005) -0.43 0.11 1 0 0
12 Adrian and Ferguson(1987) -0.37 0.15 1 0 0
13 Selvanathan(1991) -0.26 0.17 1 0 0
14 Quek(1988) -0.16 0.22 1 0 0
15 Lau(1975) -0.43 0.39 1 0 0
16 Selvanathan and Selvanathan(2004) -0.16 0.03 1 0 0
我想能够用ggplot来制作boxplot或散点图,只用于一种饮料(est),即啤酒。如果我使用这个代码>。
boxplot(est[beer=="1"] ~ Author[beer=="1"],
main="Boxplot of Bier elasticities",
xlab="Price elasticity", ylab=" ",
ylim=c(-5,3), las=1,
horizontal = TRUE)
那么我可以分别选择啤酒和烈酒,并得到三个不同的boxpl图(或直方图--这是我的目标,因为我想分别评估它们),但用ggplot我只能为所有饮料一起生成代码。
ggplot(alc3, aes(x=est, y=Author) + geom_boxplot() +
ggtitle("Price elasticities of alcohol") +
xlab("Estimates") +
ylab(" "))
我尝试生成新的变量
beer1 <- alc3$est[beer=="1"]
Author1 <- alc3$Author[beer=="1"]
但是,即使我把它们替换成ae(x=啤酒1,y=作者1)......我也得到了这个错误信息>。
Error: Aesthetics must be either length 1 or the same as the data (406): x and y"
虽然他们有相同的长度。
有什么其他方法吗?谁能给点建议,应该怎么改。
非常感谢!!Anita
你可以只过滤数据,就像你用的是 boxplot()
:
library(tidyverse)
library(ggplot2)
# note: I changed the data a bit, so that it wasn't "just beer", to make the second example work
alc3 <- tribble(~Author, ~est, ~se, ~beer, ~wine, ~spirits,
"Andrikopoulos and Loizides(2000)", -1.00, 0.18, 1, 0, 0,
"Andrikopoulos and Loizides(2000)", -0.35, 0.32, 0, 1, 0,
"Andrikopoulos et al. 1997", -1.00, 0.46, 0, 0, 1,
"Andrikopoulos et al. 1997", -1.02, 0.46, 0, 1, 1,
"Adrian and Ferguson(1987)", -0.84, 0.17, 1, 0, 0,
"Andrikopoulos et al. 1997", -0.48, 0.13, 1, 1, 0,
"Andrikopoulos et al. 1997", -0.08, 0.07, 1, 0, 1,
"Quek(1988)", -0.28, 0.03, 0, 1, 0,
"Johnson et al.(1992)", -0.14, 0.05, 1, 0, 0,
"Johnson et al.(1992)", -0.26, 0.06, 1, 0, 0,
"Selvanathan and Selvanathan(2005)", -0.43, 0.11, 0, 1, 1,
"Adrian and Ferguson(1987)", -0.37, 0.15, 1, 0, 1,
"Selvanathan(1991)", -0.26, 0.17, 1, 1, 0,
"Quek(1988)", -0.16, 0.22, 0, 1, 0,
"Lau(1975)", -0.43, 0.39, 1, 0, 1,
"Selvanathan and Selvanathan(2004)", -0.16, 0.03, 1, 0, 1)
# example with filtering:
alc3 %>%
filter(beer == 1) %>%
ggplot(aes(y=est, x=Author)) + geom_boxplot() +
ggtitle("Price elasticities of beer") +
xlab("Estimates") +
coord_flip()
# example with pivoted, tidy data and `face_wrap()`
alc3 %>%
pivot_longer(cols = 4:6, names_to = "alcohol") %>%
filter(value == 1L) %>%
ggplot(aes(y=est, x=Author)) +
geom_boxplot() +
facet_wrap(~alcohol) +
coord_flip() +
ggtitle("Price elasticities of alcohol") +
xlab("Estimates") +
ylab(" ") +
theme(axis.text.x = element_text(angle = 90))
编辑:改变顺序 forcats::fct_relevel()
:
alc3 %>%
pivot_longer(cols = 4:6, names_to = "alcohol") %>%
filter(value == 1L) %>%
mutate(alcohol = forcats::fct_relevel(alcohol, "wine", "beer", "spirits")) %>%
ggplot(aes(y=est, x=Author)) +
geom_boxplot() +
facet_wrap(~alcohol) +
coord_flip() +
ggtitle("Price elasticities of alcohol") +
xlab("Estimates") +
ylab(" ") +
theme(axis.text.x = element_text(angle = 90))
创建于2020-06-13 重读包 (v0.3.0)
如果你使用pivot_longer将数据从宽表格改成长表格,你可以制作你需要的图。请看这里 https:/tidyr.tidyverse.orgreferencepivot_longer.html。
想法是创建一个新的 "饮料 "变量,以 "啤酒"、葡萄酒和烈酒为值,然后用新的 "饮料 "变量制作ggplot。