R ggplot2:每因子面积条形图(几何图形条)-包含缺失值(几何图形文本)

R ggplot2:每因子面积条形图(几何图形条)-包含缺失值(几何图形文本),r,ggplot2,geom-bar,geom-text,R,Ggplot2,Geom Bar,Geom Text,我正在为一个林业项目做一些面积计算。 数据由1241个观测值和两个相关变量组成: MiWaReVe:20类森林类型,用数字代码缩写,采用“因子”格式。 面积:森林类型的面积,单位为公顷,采用“num”格式 以下是我的最小数据集: structure(list(Id = c(0L, 2L, 3L, 4L, 5L, 17L), MiWaReVe = structure(c(7L, 7L, 14L, 17L, 17L, 17L), .Label = c("", "0", "1.1.", "2.1."

我正在为一个林业项目做一些面积计算。 数据由1241个观测值和两个相关变量组成:

MiWaReVe:20类森林类型,用数字代码缩写,采用“因子”格式。 面积:森林类型的面积,单位为公顷,采用“num”格式

以下是我的最小数据集:

structure(list(Id = c(0L, 2L, 3L, 4L, 5L, 17L), MiWaReVe = structure(c(7L, 
7L, 14L, 17L, 17L, 17L), .Label = c("", "0", "1.1.", "2.1.", 
"2.2.1.", "2.2.2.", "2.3.1.", "2.3.2.", "3.1.1.", "3.1.2.", "3.2.1.", 
"3.2.2.", "3.2.3.", "4.1.", "4.2.", "5.1.", "5.2.", "6.", "7.", 
"8."), class = "factor"), area_ha = c(8.08759, 8.76723, 5.5033, 
1.22659, 4.31278, 8.23421), Owner = structure(c(2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("Bundesforsten", "Kommunalwald", "Privatwald", 
"Staatswald"), class = "factor"), hint_cl = structure(c(3L, 3L, 
3L, 4L, 4L, 4L), .Label = c("A", "B", "C", "D", "E", "X"), class = "factor"), 
area_in_per = c(0.216871128099877, 0.23509587657276, 0.147572624140449, 
0.032891375182969, 0.115648476721321, 0.220802786950289)), .Names = c("Id", 
"MiWaReVe", "area_ha", "Owner", "hint_cl", "area_in_per"), row.names = c(NA, 
6L), class = "data.frame")
Id MiWaReVe area_ha        Owner hint_cl area_in_per
1  0   2.3.1. 8.08759 Kommunalwald       C  0.21687113
2  2   2.3.1. 8.76723 Kommunalwald       C  0.23509588
3  3     4.1. 5.50330 Kommunalwald       C  0.14757262
4  4     5.2. 1.22659 Kommunalwald       D  0.03289138
5  5     5.2. 4.31278 Kommunalwald       D  0.11564848
6 17     5.2. 8.23421 Kommunalwald       D  0.22080279
我的目标是计算每种森林类型的总面积,并使用ggplot2构建一个显示百分比分布的条形图。我使用以下代码完成此操作:

library("ggplot2")
library("scales")


MiWaRe=read.table(file="2017_11_MiWaRe.csv", sep=";",dec="," , header=T)

str(MiWaRe)

# total area AOI
area_total=sum(MiWaRe$area_ha)


# area of each plot in % in a new column
MiWaRe=cbind(MiWaRe, "area_in_per"=MiWaRe$area_ha/area_total*100)
MiWaRe
sum(MiWaRe$`area_in_per`) # check


ggplot(data=MiWaRe, aes(x = factor(MiWaReVe), y=((area_in_per)/sum(area_in_per))))  +            
geom_bar(stat="identity")  +           
scale_y_continuous(labels = percent)
有了这段代码,我得到了一个基本版本的条形图,我需要

现在我想在我的栏上显示准确的百分比值。我尝试用以下内容扩展代码:

我通过以下方式扩展了代码:

ggplot(data=MiWaRe, aes(x = factor(MiWaReVe), y=((area_in_per)/sum(area_in_per))))  +            
geom_bar(stat="identity")  +           
scale_y_continuous(labels = percent)+
geom_text(aes(label = scales::percent((area_in_per)/sum(area_in_per)), y= ..prop.. ), stat= "count", vjust = 25)
但是(这是只发生一次的林类型),并给了我以下信息:“警告消息: 删除了包含缺失值的19行(geom_文本)。” 我已经对这个警告信息做了一些研究,但我仍然认为问题不仅仅在于显示空间太小

我也在尝试:

ggplot(data=MiWaRe, aes(x = factor(MiWaReVe), y=((area_in_per)/sum(area_in_per))))  +            
geom_bar(stat="identity")  +           
scale_y_continuous(labels = percent)+
geom_text(aes( label = scales::percent(..prop..),
             y= ..prop.. ), stat= "count", vjust = -1)
但是它

我想你肯定已经注意到我对R还是一个新手。事实上,我自己只学了一个星期的程序,但由于这里的论坛帖子,我已经解决了许多其他问题。我已经被这个问题困扰了好几个小时了。
因此,如果有人能进一步帮助我,我将不胜感激,我可以在漫长的道路上进一步掌握R。

您可以使用
ggrepel
包中的
geom_text_repel()
添加这些标签

首先,我创建了一个
area\u pc
变量,使其更简单:

library(ggplot2)
library(scales)
library(ggrepel)
library(dplyr)


MiWaRe$area_pc <- MiWaRe$area_in_per / sum(MiWaRe$area_in_per)
结果如下所示:


您可以使用
ggrepel
包中的
geom\u text\u repel()
添加这些标签

首先,我创建了一个
area\u pc
变量,使其更简单:

library(ggplot2)
library(scales)
library(ggrepel)
library(dplyr)


MiWaRe$area_pc <- MiWaRe$area_in_per / sum(MiWaRe$area_in_per)
结果如下所示:


在问题中包含a将增加您获得答案的机会。为最小可复制示例添加最小数据集…在问题中包含a将增加您获得答案的机会。为最小可复制示例添加最小数据集。。。
ggplot(data=MiWaRe, aes(x = factor(MiWaReVe), y = area_pc)) +            
  geom_bar(stat="identity")  +           
  scale_y_continuous(labels = percent) +
  geom_text_repel(data = labels, aes(x = factor(MiWaReVe),
                                     y = pc_label,
                                     label = scales::percent(pc_label)))