我正在为一个林业项目做一些面积计算。数据由1241个观察值和两个相关变量组成:
MiWaReVe: 20类森林类型,缩写为数字代码,采用“因子”格式。area_ha:一种森林类型的面积,以公顷为单位,采用"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)但是it labels only one bar (它是一种只出现一次的森林类型)并给出了以下警告消息:“警告消息:删除了19行包含缺失值(Geom_text)的行。”我已经对这条警告消息做了一些研究,但我仍然认为问题比显示空间太少更深。
我也在尝试:
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)但它doesn't work either, of course。
我想你肯定已经注意到我对R还很陌生。事实上,我自己只学习了一个星期的程序,但多亏了这里的论坛帖子,我已经能够解决许多其他问题了。我已经被这个问题困扰了好几个小时。因此,如果有人能进一步帮助我,我将非常感激,我可以让自己在漫长的道路上进一步掌握R。
发布于 2017-11-06 20:34:22
您可以使用ggrepel包中的geom_text_repel()来添加这些标签。
首先,我创建了一个area_pc变量以使其更容易:
library(ggplot2)
library(scales)
library(ggrepel)
library(dplyr)
MiWaRe$area_pc <- MiWaRe$area_in_per / sum(MiWaRe$area_in_per)然后我创建数据来添加标签:
labels <- MiWaRe %>%
group_by(MiWaReVe) %>%
summarise(pc_label = sum(area_pc))然后,只需将其添加到您先前创建的图中:
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))) 结果如下所示:

https://stackoverflow.com/questions/47127441
复制相似问题