首页 > 解决方案 > 一种方式-R中的Anova图与CI

问题描述

我进行了单向方差分析

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")

但我对这个可视化完全不满意

如何在这张照片上获得情节

想要的情节

标签: rggplot2

解决方案


那这个呢?

# 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()

产量

在此处输入图像描述


推荐阅读