【问题标题】:how to make plot scales the same when using ggplot2?使用ggplot2时如何使绘图比例相同?
【发布时间】:2012-12-04 02:55:39
【问题描述】:

我正在使用 ggplot2 绘制回归线。我还想用置信区间绘制平均点,以显示它是否显着。例如:

library("ggplot2")
library("gridExtra")
library("epicalc")
options(digits=2)

subset1 <- subset(na.omit(iris), Species == "setosa")
subset2 <- subset(na.omit(iris), Species == "versicolor")
subset3 <- subset(na.omit(iris), Species == "virginica")

meanx <- c(ci(subset1$Sepal.Length)$mean,
               ci(subset2$Sepal.Length)$mean,
               ci(subset3$Sepal.Length)$mean)

meany <- c(ci(subset1$Sepal.Width)$mean,
           ci(subset2$Sepal.Width)$mean,
           ci(subset3$Sepal.Width)$mean)

Species <- factor(c("setosa", "versicolor", "virginica"))
meanmatrix <- as.data.frame(cbind(Species, meanx, meany))

lowerx <- c(ci(subset1$Sepal.Length)$lower95ci,
            ci(subset2$Sepal.Length)$lower95ci,
            ci(subset3$Sepal.Length)$lower95ci)

upperx <- c(ci(subset1$Sepal.Length)$upper95ci,
            ci(subset2$Sepal.Length)$upper95ci,
            ci(subset3$Sepal.Length)$upper95ci)

lowery <- c(ci(subset1$Sepal.Width)$lower95ci,
            ci(subset2$Sepal.Width)$lower95ci,
            ci(subset3$Sepal.Width)$lower95ci)

uppery <- c(ci(subset1$Sepal.Width)$upper95ci,
            ci(subset2$Sepal.Width)$upper95ci,
            ci(subset3$Sepal.Width)$upper95ci)

px <- ggplot(data = meanmatrix, geom = 'blank',
             aes(y = meanx, x = meany,color = factor(Species)))
pbx <- px + 
  geom_point(size = 5) +
  geom_errorbar(aes(ymin=lowerx, ymax=upperx), colour="black", width=.1) +
  scale_color_manual(values = c("#00FFFF", "#FFFF00", "#00FF00")) +
  theme(panel.background = element_rect(fill='white', colour='red'),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "none") +
  coord_flip()


py <- ggplot(data = meanmatrix, geom = 'blank',
             aes(y = meany, x = meany,color = factor(Species)))
pby <- py + 
  geom_point(size = 5) +
  geom_errorbar(aes(ymin=lowery, ymax=uppery), colour="black", width=.1) +
  scale_color_manual(values = c("#00FFFF", "#FFFF00", "#00FF00")) +
  theme(panel.background = element_rect(fill='white', colour='red'),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "none")

p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width,
                    color = factor(Species)))

p0 <- p + 
  scale_color_manual(values = c("#00FFFF", "#FFFF00", "#00FF00")) +
  scale_linetype_manual(breaks = c("0","1"), values = c(1,2), labels = c("male", "female")) +
  geom_smooth(method = "lm",se = FALSE, size = 1.2) +
  theme(panel.background = element_rect(fill='white', colour='red'),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "none")

grid.newpage() 
pushViewport(viewport(layout = grid.layout(nrow=3, ncol=3)))
print(p0,vp = viewport(layout.pos.row = 1:2, layout.pos.col = 2:3))
print(pby,vp = viewport(layout.pos.row = 1:2, layout.pos.col = 1))    
print(pbx,vp = viewport(layout.pos.row = 3, layout.pos.col = 2:3))    

三个地块的尺度不同。我怎样才能使它们通用,以便我可以比较它们?谢谢

【问题讨论】:

  • 你的代码中使用的ci函数是什么?
  • 如何使用scale_x_continuous()scale_y_continuous() 设置坐标轴的限制?
  • @agstudy 我忘了添加库部分。 ci 是一个函数,是包 epicalc。

标签: r ggplot2


【解决方案1】:

就像 Ernest A. 评论的那样,您可以使用 scale_x_continuousscale_y_continuous 手动更改 x 轴和 y 轴的比例。只需将 breaks 参数设置为相同的值即可。

在绘图本身中绘制不确定性条或绘制包含 95% 置信区间的回归线也可能更容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-02
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2020-08-16
    • 2014-12-17
    • 1970-01-01
    相关资源
    最近更新 更多