我正在尝试绘制x轴上的年龄,y轴上的预期血清尿酸盐以及男性/白人,女性/白人,男性/黑人,女性/黑人的线图,并使用lm()函数。
goutdata <- read.table("gout.txt", header = TRUE)
goutdata$sex <- factor(goutdata$sex,levels = c("M", "F"))
goutdata$race <- as.factor(goutdata$race)
fm <- lm(su~sex+race+age, data = goutdata)
summary(fm)
ggplot(fm, aes(x= age, y = su))+xlim(30, 70) + geom_jitter(aes(age,su, colour=age)) + facet_grid(sex~race)
我尝试将facet_wrap()函数与ggplot一起使用来解决类别变量,但是我只想创建一个图。我正在尝试将geom_jitter和geom_smooth组合使用,但是我不确定如何将geom_smooth()与分类变量一起使用。任何帮助,将不胜感激。
答案 0 :(得分:3)
我们可以使用interaction()
快速创建分组,并在geom_smooth()
内执行OLS。在这里,它们被分组为一个图:
ggplot(goutdata, aes(age, su, color = interaction(sex, race))) +
geom_smooth(formula = y~x, method="lm") +
geom_point() +
hrbrthemes::theme_ipsum_rc(grid="XY")
然后扩展到各个方面:
ggplot(goutdata, aes(age, su, color = interaction(sex, race))) +
geom_smooth(formula = y~x, method="lm") +
geom_point() +
facet_wrap(sex~race) +
hrbrthemes::theme_ipsum_rc(grid="XY")
您现在对https://github.com/gdlc/STT465/blob/master/HW_4_OLS.md的#1有部分答案:-)
答案 1 :(得分:1)
您可能可以使用geom_smooth()
来显示回归线?
dat <- read.table("https://raw.githubusercontent.com/gdlc/STT465/master/gout.txt",
header = T, stringsAsFactors = F)
library(tidyverse)
dat %>%
dplyr::mutate(sex = ifelse(sex == "M", "Male", "Female"),
race = ifelse(race == "W", "Caucasian", "African-American"),
group = paste(race, sex, sep = ", ")
) %>%
ggplot(aes(x = age, y = su, colour = group)) +
geom_smooth(method = "lm", se = F, show.legend = F) +
geom_point(show.legend = F, position = "jitter", alpha = .5, pch = 16) +
facet_wrap(~group) +
ggthemes::theme_few() +
labs(x = "Age", y = "Expected serum urate level")