ggplot:绘制虚拟变量

ggplot: Plotting a dummy variable(s)

这里是新手:) 如果您能给我任何 help/advise,我将不胜感激。 我正在尝试 plot/scatterplot/boxplot/hist 我拥有的数据以进行一些目视检查,并且假设我使用其他一些命令到达了我想要的位置......但是当我尝试使用 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[beer=="1"] ~ Author[beer=="1"], 
main="Boxplot of Bier elasticities", 
xlab="Price elasticity", ylab=" ", 
ylim=c(-5,3), las=1, 
horizontal = TRUE) 

然后我可以 select beer/wine/spirits 分别得到三个不同的箱线图(或直方图 - 这是我的目标,因为我想分别评估它们)但是使用 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"]

但即使我将它们替换为 aes(x=beer1, y=Author1)....我也收到此错误消息>

Error: Aesthetics must be either length 1 or the same as the data (406): x and y"

虽然它们的长度相同。

还有其他办法吗?谁能建议应该改变什么。

非常感谢!! 梅艳芳

如果使用 pivot_longer 将数据从宽格式重塑为长格式,则可以绘制所需的图。看这里https://tidyr.tidyverse.org/reference/pivot_longer.html

想法是创建一个新的 "drinks" 变量,其中 "beer"、葡萄酒和烈酒作为值,然后使用新的 "drinks" 变量

制作 ggplot

您可以像对 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))

reprex package (v0.3.0)

于 2020-06-13 创建