我正在尝试使用ggplot2对多变量数据进行geom_point绘图,但我遇到了对数据进行颜色编码以及可视化绘图的问题。我在下面分享了我的数据:我对努力(X轴)与换发(Y轴)感兴趣,并按头发类型(头发类型:弥漫性、额叶/颞叶和/或顶点)对数据进行颜色编码。然而,调查的性质是多变量的,患者能够支持多种脱发类型(HairType 1、2和/或3)。前20名参与者的代码如下:
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"))由于患者认可的发型属于多个列,因此我无法使用以下代码在视觉上分离数据:
ggplot(data, aes(x=Effort, y=hairchange, color = hairtype????)+geom_point()如果数据以某种方式显示在显示脱发的1列中,则很容易可视化:

因此,我想知道是否有一种方法来组织数据,以允许可视化和颜色编码的3种脱发类型?我已经尝试了reshape2和熔化,但没有任何运气。我想避免创建第四类“多类型报告”,因为这蒙蔽了许多人对我想要做的洞察力的理解。
或者,建议使用替代方法来绘制此数据(密度/线状图)。我的一个想法是有四个单独的线状图-每个脱发类型(即,平均,漫射,顶点,时间)-以x轴为努力,y轴为平均感知的头发变化。
发布于 2018-10-17 03:56:10
我使用了以下代码片段:
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”栏目:
dt[, CombinedHair:=do.call(paste0,.SD), .SDcols=c(5,6,7)]如果你想绘制这个数据表的数据,它有重叠,所以我建议使用geom_jitter()函数:
ggplot(data = dt, aes(x=Effort, y=hairchange, color = CombinedHair))+geom_jitter(width = 0.1, height = 0.1)如果你想要更好的类名,我想你可以用空引号替换‘default’。

发布于 2018-10-18 02:19:32
这里有一种方法,它将位置移动到它自己的变量中(这里没有显示,但如果您愿意,可以将其映射到面、点形状或其他美学),然后根据头发类型绘制颜色,删除“其他”头发类型。
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

https://stackoverflow.com/questions/52842431
复制相似问题