Участок взаимодействия в ggplot2
Я пытаюсь сделать график взаимодействия с ggplot2
. Мой код ниже:
library(ggplot2)
p <- qplot(as.factor(dose), len, data=ToothGrowth, geom = "boxplot", color = supp) + theme_bw()
p <- p + labs(x="Dose", y="Response")
p <- p + stat_summary(fun.y = mean, geom = "point", color = "blue")
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = 1))
p <- p + opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0))
p <- p + opts(axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25))
print(p)
Как я могу составить график комбинации уровня дозы, а не только уровень дозы, который я получаю здесь? Заранее благодарим за помощь.
![enter image description here]()
Ответы
Ответ 1
Вы можете предварительно вычислить значения в своем собственном кадре данных:
toothInt <- ddply(ToothGrowth,.(dose,supp),summarise, val = mean(len))
ggplot(ToothGrowth, aes(x = factor(dose), y = len, colour = supp)) +
geom_boxplot() +
geom_point(data = toothInt, aes(y = val)) +
geom_line(data = toothInt, aes(y = val, group = supp)) +
theme_bw()
![enter image description here]()
Обратите внимание, что использование ggplot
, а не qplot
делает построение графика более понятным для более сложных графиков, подобных этим (IMHO).
Ответ 2
Вы можете вычислить свои резюме соответствующими группами (supp
):
p <- qplot(as.factor(dose), len, data=ToothGrowth, geom = "boxplot", color = supp) + theme_bw()
p <- p + labs(x="Dose", y="Response")
p <- p + stat_summary(fun.y = mean, geom = "point", color = "blue", aes(group=supp))
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = supp))
p <- p + opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0))
p <- p + opts(axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25))
print(p)
Или преобразование в синтаксис ggplot
(и объединение в одно выражение)
ggplot(ToothGrowth, aes(as.factor(dose), len, colour=supp)) +
geom_boxplot() +
stat_summary(aes(group=supp), fun.y = mean, geom="point", colour="blue") +
stat_summary(aes(group=supp), fun.y = mean, geom="line") +
scale_x_discrete("Dose") +
scale_y_continuous("Response") +
theme_bw() +
opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0),
axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25))
EDIT:
Чтобы сделать эту работу с 0.9.3, она становится Joran answer.
library("plyr")
summ <- ddply(ToothGrowth, .(supp, dose), summarise, len = mean(len))
ggplot(ToothGrowth, aes(as.factor(dose), len, colour=supp)) +
geom_boxplot() +
geom_point(data = summ, aes(group=supp), colour="blue",
position = position_dodge(width=0.75)) +
geom_line(data = summ, aes(group=supp),
position = position_dodge(width=0.75)) +
scale_x_discrete("Dose") +
scale_y_continuous("Response") +
theme_bw() +
theme(axis.title.x = element_text(size = 12, hjust = 0.54, vjust = 0),
axis.title.y = element_text(size = 12, angle = 90, vjust = 0.25))
![enter image description here]()
Ответ 3
Если вы считаете, что вам может понадобиться более общий подход, вы можете попробовать функцию rxnNorm в пакете HandyStuff (github.com/bryanhanson/HandyStuff). Отказ от ответственности: Я автор. Отказ от ответственности № 2: вариант с футляром не работает правильно, но все остальные варианты в порядке.
Вот пример использования данных ToothGrowth:
p <- rxnNorm(data = ToothGrowth, res = "len", fac1 = "dose", fac2 = "supp", freckles = TRUE, method = "iqr", fac2cols = c("red", "green"))
print(p)
![rxnNorm Demo]()
Ответ 4
намного проще. без ddply. непосредственно с ggplot2.
ggplot(ToothGrowth, aes(x = factor(dose) , y=len , group = supp, color = supp)) +
geom_boxplot() +
geom_smooth(method = lm, se=F) +
xlab("dose") +
ylab("len")