简体   繁体   中英

R ggplot - Boxplot with fill displaying different values than bargraph

I have a boxplot generated using the following code, and after checking the dataset all the values are correct here.

myplot <- inDATA %>% filter(PARAMCD=="param1") %>% 
            ggplot(aes(x=ACTARMCD,y=AVAL,fill=ACTARMCD))+
            geom_boxplot()+
            stat_summary(fun.y=mean,na.rm=TRUE,shape=25,col='black',geom='point')

箱线图 1 链接

I want to generate a second boxplot where I split the x variable into different groups by applying a different variable as a fill. I use the following code, but the values present in the graph are incorrect.

myplot <- inDATA %>% filter(PARAMCD=="param1") %>% 
            group_by(ACTARMCD, RESPFL) %>%  
            ggplot(aes(x=ACTARMCD,y=AVAL))+
            geom_boxplot(aes(fill=RESPFL))

箱线图 2 链接

However when I generate a bargraph using this code, the numbers are correct.

myplot <- inDATA %>%  
            filter(PARAMCD=="param1") %>% 
            group_by(ACTARMCD,RESPFL) %>%
            dplyr::mutate(AVAL = mean(AVAL, na.rm=TRUE)) %>% 
            ggplot(aes(x=ACTARMCD,y=AVAL,fill=RESPFL))+
            geom_bar(stat="identity",position="dodge")

条形图链接

Can anyone please help me understand what I am doing incorrectly with the second boxplot?

I ended up solving the issue by using plotly instead of ggplot. The code that worked is:

myplot <- inDATA %>% filter(PARAMCD=="param1") %>% 
plot_ly(x = ~ACTARMCD, y = ~AVAL, color = ~RESPFL, type = "box",boxmean=TRUE) %>% layout(boxmode = "group")

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