【问题标题】:R - add centroids to scatter plotR - 添加质心到散点图
【发布时间】:2014-06-21 05:09:20
【问题描述】:

我有一个数据集,有两个连续变量和一个因子变量(两个类)。我想创建一个带有两个质心(每个类一个)的散点图,其中包括 R 中的误差线。质心应该位于每个类的 x 和 y 的平均值处。

我可以使用 ggplot2 轻松创建散点图,但我不知道如何添加质心。是否可以使用 ggplot / qplot 来做到这一点?

下面是一些示例代码:

x <- c(1,2,3,4,5,2,3,5)
y <- c(10,11,14,5,7,9,8,5)
class <- c(1,1,1,0,0,1,0,0)
df <- data.frame(class, x, y)
qplot(x,y, data=df, color=as.factor(class))

【问题讨论】:

    标签: r ggplot2 scatter-plot centroid


    【解决方案1】:

    我无法获得 @jlhoward 为我工作的确切代码(特别是错误栏),所以我做了一些小改动以删除错误甚至删除警告。因此,您应该能够从头到尾运行代码,如果 @jlhoward 想要将其合并到现有答案中,那就太好了。

    centroids <- aggregate(cbind(mean.x = x, mean.y = y) ~ class, df, mean)
    gg        <- merge(df, centroids, by = "class")
    f         <- function(z) sd(z) / sqrt(length(z)) # function to calculate std.err
    se        <- aggregate(cbind(se.x = x ,se.y = y) ~ class, df, f)
    centroids <- merge(centroids, se, by = "class")    # add std.err column to centroids
    
    ggplot(gg, aes(x = x, y = y, color = factor(class))) +
      geom_point(size = 3) +
      geom_point(data = centroids, aes(x = mean.x, y = mean.y), size = 5) +
      geom_errorbar(data = centroids, 
                    aes(x = mean.x, y = mean.y, ymin = mean.y - se.y, ymax = mean.y + se.y),
                    width = 0.1) +
      geom_errorbarh(data = centroids, inherit.aes=FALSE, # keeps ggplot from using first aes
                     aes(xmin = (mean.x - se.x), xmax = (mean.x + se.x), y = mean.y,
                     height = 0.1, color = factor(class))) +
      labs(x = "Label for x-axis", y = "Label for y-axis") +
      theme(legend.title = element_blank()) # remove legend title
    

    【讨论】:

      【解决方案2】:

      这是你的想法吗?

      centroids <- aggregate(cbind(x,y)~class,df,mean)
      ggplot(df,aes(x,y,color=factor(class))) +
        geom_point(size=3)+ geom_point(data=centroids,size=5)
      

      这将创建一个单独的数据框centroids,其中包含xyclass 列,其中xy 是按类别划分的平均值。然后我们使用centroid 作为数据集添加第二个点几何图层。

      这是一个稍微有趣的版本,在聚类分析中很有用。

      gg <- merge(df,aggregate(cbind(mean.x=x,mean.y=y)~class,df,mean),by="class")
      ggplot(gg, aes(x,y,color=factor(class)))+geom_point(size=3)+
        geom_point(aes(x=mean.x,y=mean.y),size=5)+
        geom_segment(aes(x=mean.x, y=mean.y, xend=x, yend=y))
      

      编辑回应 OP 的评论。

      可以使用geom_errorbar(...)geom_errorbarh(...) 添加垂直和水平误差线。

      centroids <- aggregate(cbind(x,y)~class,df,mean)
      f         <- function(z)sd(z)/sqrt(length(z)) # function to calculate std.err
      se        <- aggregate(cbind(se.x=x,se.y=y)~class,df,f)
      centroids <- merge(centroids,se, by="class")    # add std.err column to centroids
      ggplot(gg, aes(x,y,color=factor(class)))+
        geom_point(size=3)+
        geom_point(data=centroids, size=5)+
        geom_errorbar(data=centroids,aes(ymin=y-se.y,ymax=y+se.y),width=0.1)+
        geom_errorbarh(data=centroids,aes(xmin=x-se.x,xmax=x+se.x),height=0.1)
      

      如果你想计算,比如说,95% 的置信度而不是标准。错误,替换

      f <- function(z)sd(z)/sqrt(length(z)) # function to calculate std.err
      

      f <- function(z) qt(0.025,df=length(z)-1, lower.tail=F)* sd(z)/sqrt(length(z)) 
      

      【讨论】:

      • 这太好了,谢谢。是否也可以在质心上添加水平和垂直条来表示 x 和 y 值的标准误差?
      • 谢谢!这是完美的。
      猜你喜欢
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 2019-05-15
      • 2015-10-31
      • 1970-01-01
      相关资源
      最近更新 更多