R cách đặt văn bản trên ggplot so với trục
Tôi đang cố gắng sử dụng geom_text () trong ggplot để đặt các kích thước mẫu trên biểu đồ thanh với các giới hạn xác định do ggplot xác định. Đối với hầu hết các ô của tôi, tôi đã có thể thực hiện được việc chỉ định giá trị y cho vị trí văn bản là 0. Tuy nhiên, đối với một số ô, các thanh CI kéo dài dưới 0.
Để ngăn việc in kích thước mẫu trên các thanh CI, tôi muốn tạo mã chỉ định giá trị y cho geom_text () liên quan đến lề biểu đồ xác định trục x. Ý tưởng sẽ tạo mã chung liên quan đến trục x để mã này có thể được áp dụng cho tất cả các ô của tôi bất kể giá trị nhỏ nhất của y ở đâu trong ô. Tốt nhất, tôi muốn điều chỉnh mã cốt truyện của mình để đạt được điều này nếu có thể. Cám ơn trước cho đầu vào cửa bạn!
Cơ sở dữ liệu lô đất
data <- structure(list(Site_long = structure(c(2L, 2L, 1L, 1L), .Label = c("Hanauma Bay",
"Waikiki"), class = "factor"), Shelter = structure(c(1L, 2L,
1L, 2L), .Label = c("Low", "High"), class = c("ordered", "factor"
)), mean = c(0.096818329015544, 0.140368187765273, 0.364490168863912,
0.452663275851282), sd = c(0.358269615215823, 0.442381836749624,
0.431417057945072, 0.461599742148954), lower = c(-0.13725115292546,
-0.148654612244481, 0.198658790631641, 0.337761753133984), upper = c(0.330887810956549,
0.429390987775027, 0.530321547096184, 0.56756479856858), sample_size = c(9L,
9L, 26L, 62L)), row.names = c(NA, -4L), groups = structure(list(
Site_long = structure(1:2, .Label = c("Hanauma Bay", "Waikiki"
), class = "factor"), .rows = structure(list(3:4, 1:2), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Mã lô đất
mult_compare_growth_all <- c("AB", "B", "A", "AB")
growth_plot <- ggplot(data = data, aes(fill=Shelter, y=mean, x=Site_long)) +
geom_bar(position = "dodge", stat="identity", width = .8) +
scale_x_discrete(limits = position) +
geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.8), width = .1) +
geom_text(aes(label = mult_compare_growth_all, y = data$upper), vjust = -.5, position = position_dodge(width = 0.8), size = 4) +
scale_fill_grey(name = "Shelter", start = .8, end = .2) +
labs(x = "Site", y = expression(paste("Coral growth (cm"^"2","/quarter)"))) +
theme_classic(base_size = 14.5) +
theme(text = element_text(size = 18), legend.position = "none",
axis.title.x = element_blank(),
axis.text.y = element_text(angle = 90),
axis.text.x = element_blank())
Trả lời
Bạn có thể sử dụng y = -Infđể định hướng vị trí văn bản. Để giữ cho văn bản bên trong cốt truyện, bạn phải kết hợp một vjustyếu tố nào đó Inf, nếu không, văn bản sẽ nằm ngoài cốt truyện.
Đây là mã:
ggplot(data = data, aes(fill=Shelter, y=mean, x=Site_long)) +
geom_bar(position = "dodge", stat = "identity", width = .8) +
geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.8), width = .1) +
geom_text(aes(label = mult_compare_growth_all, y = data$upper), vjust = -.5,
position = position_dodge(width = .8), size = 4) +
geom_text(aes(label = sample_size, y = -Inf), vjust = -.3,
position = position_dodge(width = .8)) +
scale_fill_grey(name = "Shelter", start = .8, end = .2) +
labs(x = "Site", y = expression(paste("Coral growth (cm"^"2","/quarter)"))) +
theme_classic(base_size = 14.5) +
theme(text = element_text(size = 18), legend.position = "none",
axis.title.x = element_blank(),
axis.text.y = element_text(angle = 90),
axis.text.x = element_blank())
Và đầu ra:
Hãy cho tôi biết nếu đây là những gì bạn đang tìm kiếm.
Có thể như một gợi ý, bạn có thể tính toán hệ số vị trí pfdựa trên giá trị tối thiểu của lowerbiến của bạn và thêm một số đại lượng điều chỉnh như sau:
library(ggplot2)
#Plot
mult_compare_growth_all <- c("AB", "B", "A", "AB")
#Factor for placement
pf <- min(data$lower)-0.02
#Code
ggplot(data = data, aes(fill=Shelter, y=mean, x=Site_long)) +
geom_bar(position = "dodge", stat="identity", width = .8) +
geom_text(aes(y=pf,label=sample_size),position = position_dodge(0.8))+
geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.8), width = .1) +
geom_text(aes(label = mult_compare_growth_all, y = upper),
vjust = -.5, position = position_dodge(width = 0.8), size = 4)+
scale_fill_grey(name = "Shelter", start = .8, end = .2) +
labs(x = "Site", y = expression(paste("Coral growth (cm"^"2","/quarter)"))) +
theme_classic(base_size = 14.5) +
theme(text = element_text(size = 18), legend.position = "none",
axis.title.x = element_blank(),
axis.text.y = element_text(angle = 90),
axis.text.x = element_blank())
Đầu ra:
vì một số lý do khi tôi lập biểu đồ này, của tôi bị đảo ngược (có thể là thứ tự yếu tố), nhưng tôi nghĩ đây là những gì bạn muốn?
ggplot(data = data, aes(fill=Shelter, y=mean, x=Site_long)) +
geom_col(aes(fill=Shelter), position = "dodge", width = 0.8) +
geom_text(aes(y=lower, group=Shelter, label = sample_size),
position = position_dodge(width = 0.8), vjust=1.5) +
geom_errorbar(aes(ymin = lower, ymax = upper),
position = position_dodge(.8), width = .1) +
geom_text(aes(label = mult_compare_growth_all, y = data$upper), vjust = -.5,
position = position_dodge(width = 0.8), size = 4) +
scale_fill_grey(name = "Shelter", start = .8, end = .2) +
labs(x = "Site", y = expression(paste("Coral growth (cm"^"2","/quarter)"))) +
theme_classic(base_size = 14.5) +
theme(text = element_text(size = 18), legend.position = "none",
axis.title.x = element_blank(),
axis.text.y = element_text(angle = 90),
axis.text.x = element_blank())
các kết quả:
Hãy để tôi phân tích điều này một chút:
Tôi đã sử dụng geom_col()vì nó được xây dựng cho loại biểu đồ thanh với chiều cao được chỉ định bởi một giá trị duy nhất. Nó cũng có thể được thực hiện với geom_bar()cách bạn đã làm.
Sau đó, geom_text()bạn cần sự groupthẩm mỹ trong đó để đặt nhãn trên các thanh chính xác và bạn phải chỉ định y=lowercái nào đặt nhãn chính xác (khó chịu) ở cuối thanh lỗi, nhưng hãy vjustdi chuyển nó xuống. Giá trị ( 1.5) có thể có một số ý nghĩa nhưng tôi không biết nó là gì và tôi đã phải loay hoay với nó để phần đó không tự động.