我有这个数据:
> head(data)
sx yd sl
1 male 35 36350
2 male 22 35350
3 male 23 28200
4 female 27 26775
5 male 30 33696
6 male 21 28516
在"sx“是性别的地方,"yd”是已经获得学位的几年了,而"sl“是薪水。我可以很容易地画出散点图。
palette(c("pink", "blue"))
plot(data$yr, data$sl, col = factor(data$sx), xlab = "Years Since Earned Highest Degree", ylab = "Salary (dollars)", main = "Salary Increases with Experience", pch = 19)
legend("topleft", legend = unique(data$sx), col = c("blue", "pink"), pch=19)
library(ggplot2)
ggplot(data, aes(x=yd,y=sl)) +
geom_point(shape=21, aes(col=sx, bg=sx)) +
xlab("Years Since Earned Highest Degree") +
ylab("Salary (dollars)") +
ggtitle("Salary Increases with Experience") +
scale_color_discrete(guide=FALSE) +
labs(fill="sex")
然而,我也根据这些数据建立了一个线性模型:
mod<-lm(sl~sx*poly(yd,2),data)
我也想不出如何把数据绘制成图表。具体来说,我希望两行对应的男性和女性数据叠加在散射图上,并进行颜色编码。我假设R有办法这样做,这样我就不用实际写出模型了。基本情节或图形答案都是好的。谢谢。
编辑:
使用geom_smooth(aes(col=sx), se = FALSE, method = "lm", formula = sl ~ sx * poly(yd, 2))
运行上面的ggplot:
ggplot(data, aes(x=yd,y=sl)) + geom_point(shape=21, aes(col=sx, bg=sx)) + geom_smooth(aes(col=sx), se = FALSE, method = "lm", formula = sl ~ sx * poly(yd, 2)) + xlab("Years Since Earned Highest Degree") + ylab("Salary (dollars)") + ggtitle("Salary Increases with Experience") + scale_color_discrete(guide=FALSE)+ labs(fill="sex")
返回此错误:
Error in model.frame.default(formula = formula, data = data, weights = weight, :
variable lengths differ (found for '(weights)')
Error in if (nrow(layer_data) == 0) return() : argument is of length zero
发布于 2017-11-16 19:50:08
我无法找到一种ggplot方法来完成它,所以下面是基本的绘图方法:
palette(c("pink", "blue"))
plot(data$yr, data$sl, col = factor(data$sx), xlab = "Years Since Earned Highest Degree", ylab = "Salary (dollars)", main = "Salary Increases with Experience", pch = 19)
legend("topleft", legend = unique(data$sx), col = c("blue", "pink"), pch=19)
lines(seq(0,25,0.1), predict.lm(quad, data.frame(yd = seq(0,25,0.1), sx = "female", stringsAsFactors = TRUE)),col="pink", lwd = 5)
lines(seq(0,25,0.1), predict.lm(quad, data.frame(yd = seq(0,25,0.1), sx = "male", stringsAsFactors = TRUE)),col="blue", lwd = 5)
两次对线路的调用是解决方案。如果有人有这样做的方式,我会非常感激它,因为它看起来好多了。
发布于 2017-11-16 21:07:37
data = data.frame(sx = c("male", "male", "male", "female", "male", "male"),
yr = c(35, 22, 23, 27, 30, 21),
sl = c(36350, 35350, 28200, 26775, 33696, 28516))
ggplot(data, aes(x=yr,y=sl)) +
geom_point(shape=21, aes(col=sx, bg=sx)) +
geom_smooth(aes(color = sx), se = FALSE, method = "lm", formula = y ~ poly(x, 2)) +
xlab("Years Since Earned Highest Degree") +
ylab("Salary (dollars)") +
ggtitle("Salary Increases with Experience") +
scale_color_discrete(guide=FALSE)+ labs(fill="sex")
这是你想要的吗?如果你有更多的女性数据,你应该得到个别的配合。现在sum(data$sx == 'female')
是1,不可能有一个多项式拟合。
例如,尝试:
data = data.frame(sx = c("male", "male", "male", "female", "male", "male", "female", "female", "female"),
yr = c(35, 22, 23, 27, 30, 21, 25, 18, 29),
sl = c(36350, 35350, 28200, 26775, 33696, 28516, 27402, 31492, 23195))
这应该能行。
https://stackoverflow.com/questions/47337702
复制相似问题