【发布时间】:2017-04-25 15:30:24
【问题描述】:
我正在制作多个散点图来显示交互。我使用 reshape2 包中的 melt 函数使我的数据看起来像这样:
head(wage)
money educ exper tenure nonwhite female married numdep smsa Region Industry
1 3.10 11 2 0 White Female Notmarried 2 1 west other
2 3.24 12 22 2 White Female Married 3 1 west services
3 3.00 11 2 0 White Male Notmarried 2 0 west trade
4 6.00 8 44 28 White Male Married 0 1 west clerocc
5 5.30 12 7 2 White Male Married 1 0 west other
6 8.75 16 9 8 White Male Married 0 1 west profserv
test1 = wage %>% select(money, educ, female, nonwhite, married, smsa, Region, Industry)
test1a = melt(test1, id.vars= c('money', 'educ'))
head(test1a)
money educ variable value
1 3.10 11 female Female
2 3.24 12 female Female
3 3.00 11 female Male
4 6.00 8 female Male
5 5.30 12 female Male
6 8.75 16 female Male
tail(test1a)
money educ variable value
3151 5.65 12 Industry construc
3152 15.00 16 Industry profserv
3153 2.27 10 Industry trade
3154 4.67 15 Industry construc
3155 11.56 16 Industry nondur
3156 3.50 14 Industry profserv
我使用的ggplot函数是:
ggplot(test1a, aes(educ,money, col = value )) + geom_point()+
facet_wrap(~ variable) + geom_smooth(method = 'lm', se = FALSE) +
theme(legend.position="none")
这给了我以下情节:
这正是我正在寻找的,除了我希望所有 6 个地块都具有相同的配色方案。换句话说,我希望所有 6 个图都具有与左上角完全相同的绿色/黄色图。
有什么建议吗?
【问题讨论】:
-
您在
variable和value列中似乎有不止一种测量类型。例如,您至少在这些列中同时具有性别和行业。如果您在融化之前向我们展示您的数据样本(将dput(data_sample)的输出粘贴到您的问题中)并告诉使用您打算使用variable和value实际表示的内容,我们可以帮助您正确地塑造您的数据。 -
@eipi10 感谢您的建议,我添加了更多代码。
-
基本上我只是不喜欢配色方案,因为它很难看。我想做的另一件事是让每个点变黑,但每条回归线都变成不同的颜色?只是为了让这些交互更容易看到。
标签: r plot ggplot2 data-science