首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用2个变量对ggplot boxplot点进行颜色编码?

如何用2个变量对ggplot boxplot点进行颜色编码?
EN

Stack Overflow用户
提问于 2019-06-19 02:25:23
回答 1查看 593关注 0票数 0

我有一个方框图,显示干预前后的分数。我想点几个不同颜色的点,基于另一个分数。

换句话说,假设10名受试者在干预前的得分为(8,9,10,8,9,10,8,9,10,8,10,8),干预后的得分为(12,9,12,8,9,10,9,10,10,10)。现在,使用标准的ggplot盒子图,我将组1和组2显示为具有不同颜色的盒子图。

代码语言:javascript
复制
PreopRespondersBoxPlotLDS <- ggboxplot(data=PreopResponders, 
x="Response",y="RespondersVsNonPreopLDS.preop", color = "Response", palette = 
"jco", add = "jitter") + labs(title = "Left Dorsal Striatum Connectivity to the Right Occipital Cortex", y = "Connectivity")
 + theme(axis.title.x = element_blank(), legend.position="none", legend.title = element_blank(), plot.title = element_text(hjust = 0.5))

我没有收到错误消息,我只是希望能够使用额外的颜色来编写二进制变量的代码。

EN

回答 1

Stack Overflow用户

发布于 2019-06-19 02:57:38

以下是问题所要求的内容。它使用geom_boxplot,而不是ggboxplot。并通过对geom_jitter的调用中的二进制条件对数据进行子集。此条件很容易成为二进制数据帧变量。

另请注意,我在图形标题中包含了换行符"\n"

代码语言:javascript
复制
library(ggplot2)

g <- ggplot(PreopResponders, aes(x = Response, y = RespondersVsNonPreopLDS.preop,
                                 color = Response)) + 
              geom_boxplot() +
              geom_jitter(data = subset(PreopResponders, Age > 50), 
                          color = "black",
                          width = 0.25, height = 0.25) +
              labs(title = "Left Dorsal Striatum Connectivity \nto the Right Occipital Cortex", y = "Connectivity") +
              theme(axis.title.x = element_blank(), 
                    legend.position = "none", 
                    legend.title = element_blank(), 
                    plot.title = element_text(hjust = 0.5))

g

数据创建代码。

代码语言:javascript
复制
PreopResponders <- data.frame(Response = factor(rep(c("Before", "After"), each = 10), labels = c("Before", "After")),
                              RespondersVsNonPreopLDS.preop = c(c(8,9,10,8,9,10,8,9,10,8),
                                                                c(12,9,12,8,9,10,10,9,10,10)))
set.seed(1234)
PreopResponders$Age <- sample(18:100, 20, TRUE)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56654984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档