1
votes

newbie here:) I would appreciate any help/advise you could give me. I am trying to plot/scatterplot/boxplot/hist the data I have for some visual inspection, and lets say I got where I wanted to be, with some other commands...but then when I tried the same with ggplot I cannot get to the end of it.

This is part of my data "alc3"> with dummy variables for each beverage type

                              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 

I want to be able to crate boxplot or scatterplots, with ggplot for only one beverage(est), i.e. beer. If I use this code>

boxplot(est[beer=="1"] ~ Author[beer=="1"], 
main="Boxplot of Bier elasticities", 
xlab="Price elasticity", ylab=" ", 
ylim=c(-5,3), las=1, 
horizontal = TRUE) 

Then I can select beer/wine/spirits respectively and get three different boxplots (or histograms - which is my goal, since I want to assess them separately) but with ggplot i can generate the code only for all beverages together.

 ggplot(alc3, aes(x=est, y=Author) + geom_boxplot() +
  ggtitle("Price elasticities of alcohol") + 
  xlab("Estimates") +
  ylab(" ")) 

I tried to generate new variables

beer1 <- alc3$est[beer=="1"] 
Author1 <- alc3$Author[beer=="1"]

But even when I replace them in aes(x=beer1, y=Author1)....i get this error message>

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

although they have the same length.

Is there any other way? Could anyone advise what should be changed.

Many thanks!! Anita

2
Just partially. Not exactly what I was looking for, but there were useful information and now I understand the error message. Thank you!!Anita C.

2 Answers

0
votes

If you reshape the data from wide form to long form using pivot_longer, you can make the plots that you need. See here https://tidyr.tidyverse.org/reference/pivot_longer.html

Idea is to create a new "drinks" variable with "beer", wine and spirits as values and then make ggplot with the new "drinks" variable

0
votes

You could just filter the data, as you did with 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))

EDIT: Changing the order with 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))

Created on 2020-06-13 by the reprex package (v0.3.0)