首页 > 解决方案 > 在 R 中使用质心的条形图

问题描述

我想使用质心位置创建几个条形图来定位它们。我已经有一个要与之结合的多边形的绘图代码。这些来自平面图,而不是世界各地的地理区域。不幸的是,我有几点与下面链接中的示例不同,后者只有三点。如何使用 ggplot2 绘制此图以涵盖我的所有观点?这是我想将条形图添加到的示例 ggplot 代码:

ggplot() + theme_minimal()+ theme(plot.title = element_text(hjust = 0.5,face="bold",size=14),line=element_line(size=0.2),
                              panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                              axis.text.x=element_blank(),axis.text.y=element_blank(),strip.text = element_text(size=11, face = "bold"),axis.title.x = element_blank(),
                              axis.title.y = element_blank(),legend.title=element_text(size=9, face = "bold"))+
  geom_polygon(aes(x =long, y = lat, group = group), size=0.5,color="black",fill="black",alpha=0.2, data =shp1_data.frame)+
  geom_polygon(aes(x =long, y = lat, group = group), size=0.5,color="white",fill="black",alpha=0.2, data =shp4_data.frame)

地理位置条形图示例

附上图片的代码

head(read.table("Sampledata.txt",header=T),15)
   Location_i variable      value        x         y
1        A005     Head  0.0000000 1238.509 -970.4791
2        A005     Hair  0.0000000 1238.509 -970.4791
3        A005    Knees  0.1666667 1238.509 -970.4791
4        A005     Ears  0.0000000 1238.509 -970.4791
5        A005    Heart  0.0000000 1238.509 -970.4791
6        A005    Mouth -0.1666667 1238.509 -970.4791
7        A005     Toes  0.0000000 1238.509 -970.4791
8        A028     Head  0.0000000 1538.509 -870.4791
9        A028     Hair  0.0000000 1538.509 -870.4791
10       A028    Knees  0.0000000 1538.509 -870.4791
11       A028     Ears  0.0000000 1538.509 -870.4791
12       A028    Heart  1.0000000 1538.509 -870.4791
13       A028    Mouth -1.0000000 1538.509 -870.4791
14       A028     Toes  0.0000000 1538.509 -870.4791
15       A030     Toes  0.0000000 1738.509 -870.6731

我已经在我的驱动器中放了一个数据样本,其中包含 x 和 y 位置字段、值、可变广告位置 ID。该图使用变量字段围绕每个质心 (location_i) 分组。

##To read in the text file after downloading
  A1<-read.table("Sampledata.txt",header=T)

标签: rggplot2bar-chartpolygonerrorbar

解决方案


推荐阅读