简体   繁体   English

R ggplot - 填充显示与条形图不同的值的箱线图

[英]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.我想生成第二个箱线图,通过应用不同的变量作为填充,我将 x 变量分成不同的组。 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.我最终通过使用 plotly 而不是 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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