-1

I am having trouble creating a side-by-side box plot comparing the prices of the two types from this data-frame. I am not sure where to start but I am assuming I would need to somehow filter out one of the "Types" to display one value of the two types.

    Price  Type
  1  200  Premium
  2  215  Premium
  3  215  Premium
  4  220  Premium
  5  225  Premium
  6  280  Premium
  7  60   Standard
  8  60   Standard  
  9  59   Standard
  10 55   Standard
  11 52   Standard
  12 65   Standard
  13 60   Standard
  14 60   Standard
  15 50   Standard
Eqeus
  • 1
  • 1
  • Does this answer your question? [Plot multiple boxplot in one graph](https://stackoverflow.com/questions/14604439/plot-multiple-boxplot-in-one-graph) – Nikolay Shebanov Aug 25 '20 at 11:32

2 Answers2

0

No need to filter. ggplot can handle it like this:

library(ggplot2)
ggplot(df) + geom_boxplot(aes(x = Type, y = Price, colour = Type))

enter image description here

Or with R standard libraries:

boxplot(Price ~ Type, df)

enter image description here

Edo
  • 7,567
  • 2
  • 9
  • 19
0

I would suggest next ggplot2 approach. Your data is in long format so you can display Type on x-axis. Next the code:

library(ggplot2)
#Data
df <- structure(list(Price = c(200L, 215L, 215L, 220L, 225L, 280L, 
60L, 60L, 59L, 55L, 52L, 65L, 60L, 60L, 50L), Type = c("Premium", 
"Premium", "Premium", "Premium", "Premium", "Premium", "Standard", 
"Standard", "Standard", "Standard", "Standard", "Standard", "Standard", 
"Standard", "Standard")), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15"))

The code:

#Plot
ggplot(data=df,aes(x=Type,y=Price,color=Type))+
  facet_wrap(.~Type,scales = 'free')+
  geom_boxplot()+
  theme_bw()+
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84