[英]one way -Anova plot in R with CI
我執行單向anova
mydat=structure(list(Price = c(1480000L, 1480000L, 1035000L, 1480000L,
1465000L, 689000L, 611000L, 611000L, NA, 855000L, 855000L, NA,
1480000L, 1035000L, NA, 1465000L, 850000L), Regionname = structure(c(2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Eastern Victoria", "Northern Metropolitan"), class = "factor")), .Names = c("Price",
"Regionname"), class = "data.frame", row.names = c(NA, -17L))
現在我嘗試創建情節
require(ggplot2)
ggplot(mydat, aes(x = Regionname, y = Price)) +
geom_boxplot(fill = "grey80", colour = "blue") +
scale_x_discrete() + xlab("Regionname") +
ylab("Price")
但我完全不滿意這種可視化
如何在這張照片上得到情節
那這個呢?
# make a simple regression first
s1 <- summary(lm(Price ~ Regionname + 0, mydat)) # note: w/o constant!
# of which you can create a model frame
m1 <- data.frame(coef=coef(s1)[1, 1], se=coef(s1)[1, 2], mn="Eastern Victoria")
m2 <- data.frame(coef=coef(s1)[2, 1], se=coef(s1)[2, 2], mn="Northern Metropolitan")
mf <- rbind(m1, m2)
i95 <- - qnorm((1 - .95) / 2) # significance 95%
library(ggplot2)
ggplot(mf, aes(color=mn)) +
geom_pointrange(aes(x=mn, y=coef, ymin=coef - se*i95, ymax=coef + se*i95), shape=0) +
geom_errorbar(aes(x=mn, ymin=coef - se*i95, ymax=coef + se*i95), width=.2) +
geom_line(aes(y=coef, x=mn, group=1)) +
scale_color_manual(values=rep("dark red", 2)) +
labs(x="Regionname", y="Price") +
guides(color=FALSE) +
theme_bw()
產量
我希望這有幫助,給定的數據不包含關於第三組'西部大城市'的任何信息,因此僅針對兩組繪制:
library(tidyverse)
mydat %>%
group_by(Regionname) %>%
summarise( mean = mean(Price, na.rm=TRUE),#you can choose a different summay stats here like median etc.
sd = sd(Price, na.rm=TRUE),
n = n(),
se=sd/sqrt(n),
ci = qt(0.975, df=n-1)*se) %>% ##I have taken 95% Confidence interval, you can decide yourself
ggplot(aes(x=Regionname, y=mean, group = factor(1))) +
geom_line() + ##connecting lines between the error bars
geom_point() +
geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci), width=.1) + ##Errorbar instead of boxplot as per your requirement
ggtitle("Figure Mean Score by Regionname")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.