我使用 ggplot 創建了許多不同的條形圖,使用以下資料和代碼:

library(ggplot)
library(gridExtra)
# Import percentage change dataset
LC_Pct_Change <- read.csv("LC_%_Change_SA.csv", header=T)
# Create plots for relevant periods
PC_1930_1960 <- ggplot(LC_Pct_Change, aes(x=Land_Cover_Category, y=Change_1930_1960))
geom_bar(stat="identity", fill=ifelse(LC_Pct_Change$Change_1930_1960<0,"darksalmon", "darkseagreen2"), show.legend = FALSE)
geom_text(aes(label = round(Change_1930_1960, 1), hjust = 0.5, vjust = ifelse(Change_1930_1960 < 0, 1.5, -1)), size = 2.5)
ggtitle("1930-1960")
xlab("Land Cover")
ylab("% Change")
theme_bw()
scale_x_discrete(limits = c("W", "R", "G", "A", "U"))
# Repeated the above for each period
# Then combine into a single plot to export
PC_All <- grid.arrange(PC_1930_1960, PC_1960_1990, PC_1990_2000, PC_2000_2007,
PC_2007_2015, PC_2015_2020, PC_1930_2020, ncol=3)
我得到的代碼在該geom_text行的條形上方和下方添加了標簽,如下所示:
相反,我希望它們位于條形的中心(水平和垂直)。我在網上找到的所有示例都只針對具有正值的條形圖——即使如此,我也嘗試了幾種方法,但我在網上看到的最常見的方法是添加類似于position = position_stack(vjust = .5)into的內容geom_text。但我無法讓它作業。
編輯:添加資料集:
LC_Pct_Change <- structure(list(
Land_Cover_Category = c("W", "R", "G", "A", "U"),
Change_1930_1960 = c(1.984932164, -3.628139858, -1.446064637, 2.20879849, 0.880473841),
Change_1960_1990 = c(1.56838857, -7.920867205, -1.967420952, 6.078769876, 2.241129711),
Change_1990_2000 = c(
1.54170708,
0.909136716, -7.092356282, -2.215064057, 6.856576543
),
Change_2000_2007 = c(-1.244727434, 5.104227969, -0.328826854, 3.025170368, -6.555844049),
Change_2007_2015 = c(1.254510492, -6.750375486, 5.846903583, -2.837464028, 2.486425439),
Change_2015_2020 = c(0.998194067, -0.084741498, -0.2985041, -1.250054436, 0.635105966),
Change_1990_2020 = c(2.549684204, -0.821752298, -1.872783653, -3.277412153, 3.4222639),
Change_1930_2020 = c(6.103004939, -12.37075936, -5.286269243, 5.010156213, 6.543867451)
), class = "data.frame", row.names = c(NA, -5L))
uj5u.com熱心網友回復:
y除了通過@lab_rat_kid 概述的美學將標簽放置在條中間的選項之外,您還可以position_stack像這樣使用來實作您想要的。
使用一些虛假的示例資料:
library(ggplot2)
LC_Pct_Change <- data.frame(
Land_Cover_Category = c("W", "R", "G", "A", "U"),
Change_1930_1960 = seq(-2, 2, length.out = 5)
)
ggplot(LC_Pct_Change, aes(x = Land_Cover_Category, y = Change_1930_1960))
geom_bar(stat = "identity", fill = ifelse(LC_Pct_Change$Change_1930_1960 < 0, "darksalmon", "darkseagreen2"), show.legend = FALSE)
geom_text(aes(label = round(Change_1930_1960, 1)),
position = position_stack(vjust = .5), size = 2.5)
ggtitle("1930-1960")
xlab("Land Cover")
ylab("% Change")
theme_bw()
scale_x_discrete(limits = c("W", "R", "G", "A", "U"))

更新不確定將上面的代碼與您的真實資料一起使用時有什么問題。但是根據您的真實資料,我稍微簡化了您的代碼,例如,我沒有復制和粘貼相同的代碼,恕我直言容易出錯,我將繪圖代碼放在一個函式中。之后,我使用lapply回圈遍歷要繪制的列以創建繪圖串列。然后可以繪制此串列grid.arrange以創建最終圖表。不幸的是grid.arrange,不允許將地塊作為串列傳遞。這就是為什么我不得不使用do.call來實作這一點。
注意:當然你可以繼續沒有lapplyand do.call,例如你可以做PC_1930_1960 <- plot_fun("Change_1930_1960")一個一個地創建你的情節。
此外,我沒有提供填充顏色作為引數,而是使用fillaes 并通過scale_fill_manual. 再次這樣做通常不太容易出錯。
library(ggplot2)
plot_fun <- function(col) {
title <- gsub("^Change_", "", col)
title <- gsub("_", "-", title)
ggplot(LC_Pct_Change, aes(x = Land_Cover_Category, y = .data[[col]]))
geom_col(aes(fill = .data[[col]] < 0))
scale_fill_manual(values = c("FALSE" = "darkseagreen2", "TRUE" = "darksalmon"), guide = "none")
geom_text(aes(label = round(.data[[col]], 1)),
position = position_stack(vjust = .5), size = 2.5)
ggtitle(title)
xlab("Land Cover")
ylab("% Change")
theme_bw()
scale_x_discrete(limits = c("W", "R", "G", "A", "U"))
}
cols_to_plot <- names(LC_Pct_Change)[grepl("^Change", names(LC_Pct_Change))]
plot_list <- lapply(cols_to_plot, plot_fun)
names(plot_list) <- cols_to_plot
library(gridExtra)
do.call("grid.arrange", c(plot_list, ncol = 3))

uj5u.com熱心網友回復:
geom_text(aes(label = round(Change_1930_1960, 1), hjust = 0.5, y= (Change_1930_1960/2)), size = 2.5)
這應該可以,但是您沒有以可用的形式提供資料(請不要拍照),所以我無法測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/527528.html
標籤:rggplot2条形图
