简体   繁体   中英

GGplot Boxplot vs boxplot

I am trying to do a boxplot with GGplot and I get only one box when I do a ggplot like

ggplot(data_frame, aes(x=probs, y=values)) + 
  geom_boxplot(color="red", fill="orange", alpha=0.5)

while I should be getting 3 boxes when I do the normal plot:

boxplot(values ~ probs, data = data_frame,
        xlab = "Probabilities", 
        ylab = "Values (1Q, Mean, Median, StdDev, 3Q)", 
        main = "1Q, Mean, Median, StdDev, 3Q", 
        col = c("green","yellow","purple"))

Sample data:
> head(data_frame,20)
   values probs
1  16.000   0.3
2  18.000   0.3
3  18.000   0.3
4   3.550   0.3
5  20.000   0.3
6  27.000   0.5
7  30.000   0.5
8  30.000   0.5
9   3.873   0.5
10 33.000   0.5
11 46.000   0.8
12 48.000   0.8
13 48.000   0.8
14  3.098   0.8
15 50.000   0.8

Any pointers is much appreciated. Thanks!

Geom_boxplot's X axis must be categorical to get it like you want to. One solution is to transform "probs" as character:

ggplot(data_frame, aes(x=as.character(probs), y=values)) + 
  geom_boxplot(color="red", fill="orange", alpha=0.5)

Hopefully this is what you are looking for...

ggplot(df, aes(x=factor(probs), y=values)) + 
    geom_boxplot(color="red", fill="orange", alpha=0.5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM