首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ggplot2中使用美学映射可视化多变量数据

在ggplot2中使用美学映射可视化多变量数据
EN

Stack Overflow用户
提问于 2018-10-17 03:08:23
回答 2查看 89关注 0票数 0

我正在尝试使用ggplot2对多变量数据进行geom_point绘图,但我遇到了对数据进行颜色编码以及可视化绘图的问题。我在下面分享了我的数据:我对努力(X轴)与换发(Y轴)感兴趣,并按头发类型(头发类型:弥漫性、额叶/颞叶和/或顶点)对数据进行颜色编码。然而,调查的性质是多变量的,患者能够支持多种脱发类型(HairType 1、2和/或3)。前20名参与者的代码如下:

代码语言:javascript
复制
Figure3Data = structure(list(MonthsMassage = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1), 
MinutesPerDayMassage = c("0-10 minutes daily", "0-10 minutes daily", 
"0-10 minutes daily", "0-10 minutes daily", "0-10 minutes daily", 
"0-10 minutes daily", "0-10 minutes daily", "0-10 minutes daily", 
"0-10 minutes daily", "0-10 minutes daily", 
"11-20 minutes daily", "11-20 minutes daily", "11-20 minutes daily", 
"0-10 minutes daily", "0-10 minutes daily", "0-10 minutes daily", 
"0-10 minutes daily", "0-10 minutes daily", "0-10 minutes daily", 
"0-10 minutes daily"), Minutes = c(5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 15, 15, 15, 5, 5, 5, 5, 5, 5, 5), hairchange = c(-1, -1, 0, 
-1, 0, -1, -1, 0, 0, -1, 0, -1, -1, 0, 0, -1, 0, -1, 0, -1), 
HairType1 = c("Templefrontal", "Templefrontal", "Templefrontal", 
"Templefrontal", "Templefrontal", "Templefrontal", "Templefrontal", 
"other", "Templefrontal", "Templefrontal", "Templefrontal", 
"Templefrontal", "Templefrontal", "Templefrontal", "Templefrontal", 
"Templefrontal", "Templefrontal", "Templefrontal", "Templefrontal", 
"Templefrontal"), HairType2 = c("other", "other", "other", 
"other", "other", "other", "other", "other", "other", "Vertexthinning", 
"Vertexthinning", "other", "Vertexthinning", "other", "other", 
"Vertexthinning", "other", "Vertexthinning", "Vertexthinning", 
"other"), HairType3 = c("other", "Diffusethinning", "other", 
"Diffusethinning", "other", "other", "Diffusethinning", "Diffusethinning", 
"Diffusethinning", "other", "Diffusethinning", "Diffusethinning", 
"other", "other", "Diffusethinning", "Diffusethinning", "other", 
"Diffusethinning", "Diffusethinning", "Diffusethinning"), 
Effort = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.5, 2.5, 
2.5, 2.5, 2.5, 2.5, 2.5), EffortGroup = c("<5", "<5", "<5", 
"<5", "<5", "<5", "<5", "<5", "<5", "<5", "<5", "<5", "<5", 
"<5", "<5", "<5", "<5", "<5", "<5", "<5")), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

由于患者认可的发型属于多个列,因此我无法使用以下代码在视觉上分离数据:

代码语言:javascript
复制
ggplot(data, aes(x=Effort, y=hairchange, color  = hairtype????)+geom_point()

如果数据以某种方式显示在显示脱发的1列中,则很容易可视化:

因此,我想知道是否有一种方法来组织数据,以允许可视化和颜色编码的3种脱发类型?我已经尝试了reshape2和熔化,但没有任何运气。我想避免创建第四类“多类型报告”,因为这蒙蔽了许多人对我想要做的洞察力的理解。

或者,建议使用替代方法来绘制此数据(密度/线状图)。我的一个想法是有四个单独的线状图-每个脱发类型(即,平均,漫射,顶点,时间)-以x轴为努力,y轴为平均感知的头发变化。

EN

回答 2

Stack Overflow用户

发布于 2018-10-17 03:56:10

我使用了以下代码片段:

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

dt <- data.table(MonthsMassage = c(0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1), 
                      MinutesPerDayMassage = c("0-10 minutes daily", "0-10 minutes daily", 
                                               "0-10 minutes daily", "0-10 minutes daily", "0-10 minutes daily", 
                                               "0-10 minutes daily", "0-10 minutes daily", "0-10 minutes daily", 
                                               "0-10 minutes daily", "0-10 minutes daily", 
                                               "11-20 minutes daily", "11-20 minutes daily", "11-20 minutes daily", 
                                               "0-10 minutes daily", "0-10 minutes daily", "0-10 minutes daily", 
                                               "0-10 minutes daily", "0-10 minutes daily", "0-10 minutes daily", 
                                               "0-10 minutes daily"),
                      Minutes = c(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 15, 15, 15, 5, 5, 5, 5, 5, 5, 5),
                      hairchange = c(-1, -1, 0, -1, 0, -1, -1, 0, 0, -1, 0, -1, -1, 0, 0, -1, 0, -1, 0, -1), 
                      HairType1 = c("Templefrontal", "Templefrontal", "Templefrontal", 
                                      "Templefrontal", "Templefrontal", "Templefrontal", "Templefrontal", 
                                      "other", "Templefrontal", "Templefrontal", "Templefrontal", 
                                      "Templefrontal", "Templefrontal", "Templefrontal", "Templefrontal", 
                                      "Templefrontal", "Templefrontal", "Templefrontal", "Templefrontal", 
                                      "Templefrontal"),
                      HairType2 = c("other", "other", "other", "other", "other", "other", "other", "other",
                                    "other", "Vertexthinning", "Vertexthinning", "other", "Vertexthinning",
                                    "other", "other", "Vertexthinning", "other", "Vertexthinning", 
                                    "Vertexthinning", "other"),
                      HairType3 = c("other", "Diffusethinning", "other", "Diffusethinning", "other", "other",
                                    "Diffusethinning", "Diffusethinning", "Diffusethinning", "other", 
                                    "Diffusethinning", "Diffusethinning", "other", "other", "Diffusethinning", 
                                    "Diffusethinning", "other", "Diffusethinning", "Diffusethinning", "Diffusethinning"), 
                      Effort = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5), 
                      EffortGroup = c("<5", "<5", "<5", "<5", "<5", "<5", "<5", "<5", "<5", "<5", "<5", "<5", "<5", 
                                      "<5", "<5", "<5", "<5", "<5", "<5", "<5"))

你可以创建一个全新的栏目,将这三种发型组合在一起,只需将第5、6和7栏粘贴在一起,作为新的“combinedHair”栏目:

代码语言:javascript
复制
dt[, CombinedHair:=do.call(paste0,.SD), .SDcols=c(5,6,7)]

如果你想绘制这个数据表的数据,它有重叠,所以我建议使用geom_jitter()函数:

代码语言:javascript
复制
ggplot(data = dt, aes(x=Effort, y=hairchange, color  = CombinedHair))+geom_jitter(width = 0.1, height = 0.1)

如果你想要更好的类名,我想你可以用空引号替换‘default’。

票数 0
EN

Stack Overflow用户

发布于 2018-10-18 02:19:32

这里有一种方法,它将位置移动到它自己的变量中(这里没有显示,但如果您愿意,可以将其映射到面、点形状或其他美学),然后根据头发类型绘制颜色,删除“其他”头发类型。

代码语言:javascript
复制
library(tidyverse)
Figure3Data_long <- Figure3Data %>%
  gather(location, hairtype, HairType1:HairType3) %>%
  filter(hairtype != "other")

ggplot(Figure3Data_long,
       aes(Effort, hairchange, color = hairtype)) +
  # geom_point() +  
  geom_jitter(width = 0.03, height = 0.01)  # illustrative to show overplots 

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52842431

复制
相关文章

相似问题

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