我正在使用ggplot2一個小資料框進行繪圖df。我的資料幀擁有一批變數Letter和兩個數值變數X與Y(我包括dput()版本df在這篇文章的末尾)。
當我嘗試對齊頂部條形的標簽時,出現了我的主要問題。由于我的設計,一個變數需要顯示在右側,另一個需要顯示在左側。這就是我將一些值乘以 -1 的原因。這是我的情節的代碼和輸出:
library(tidyverse)
library(ggplot2)
#Plot
df %>%
pivot_longer(-c(Letter)) %>%
mutate(value=ifelse(name=='X',value*-1,value)) %>%
ggplot(aes(x=Letter,y=value,fill=name))
geom_bar(stat = 'identity',color='black',alpha=0.7)
geom_text(aes(label=format(abs(value),big.mark = '.')),
size=3,fontface='bold')
scale_x_discrete(limits = rev(unique(df$Letter)))
scale_y_continuous(labels = function(x) scales::comma(abs(x)),
breaks = scales::pretty_breaks(10))
coord_flip()
和輸出:

如您所見,情節很好,但問題出在標簽上。在左右兩側,標簽的一部分在條形內部,另一部分在外部。我想在每個條形頂部的兩側都有標簽。我不知道這是否可行,因為有些值是正的,有些是負的。我曾嘗試加入hjust到geom_text()這僅適用于左側:
#Plot 1
df %>%
pivot_longer(-c(Letter)) %>%
mutate(value=ifelse(name=='X',value*-1,value)) %>%
ggplot(aes(x=Letter,y=value,fill=name))
geom_bar(stat = 'identity',color='black',alpha=0.7)
geom_text(aes(label=format(abs(value),big.mark = '.')),
size=3,fontface='bold',
hjust=1)
scale_x_discrete(limits = rev(unique(df$Letter)))
scale_y_continuous(labels = function(x) scales::comma(abs(x)),
breaks = scales::pretty_breaks(10))
coord_flip()
輸出:

我想找到一種方法使兩側的標簽與各自條的頂部對齊。
非常感謝您的幫助。在dput()我的資料是下一個:
#Data
df <- structure(list(Letter = c("A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S"),
X = c(3099, 14201, 18901, 17351, 29498, 28928, 32284, 31536,
25334, 20008, 39242, 35856, 43317, 53765, 44652, 32876, 26142,
18567, 8836), Y = c(9482, 22669, 11159, 9506, 12113, 12805,
10570, 10199, 9190, 9217, 10674, 10894, 11986, 15664, 12793,
10552, 8050, 8609, 7717)), row.names = c(NA, -19L), class = c("tbl_df",
"tbl", "data.frame"))
uj5u.com熱心網友回復:
您實際上可以將 設定hjust為美學變數,而不是為所有標簽提供相同的 hjust 值。只是通過執行給它一個正值的1負值和0hjust = value < 0 內的aes呼叫-所得邏輯向量將被隱式轉換為1和0:
df %>%
pivot_longer(-c(Letter)) %>%
mutate(value = ifelse(name == 'X', value * -1, value)) %>%
ggplot(aes(x = Letter, y = value, fill = name))
geom_bar(stat = 'identity', color = 'black', alpha = 0.7)
geom_text(aes(label = format(abs(value), big.mark = '.'), hjust = value < 0),
size = 3, fontface = 'bold')
scale_x_discrete(limits = rev(unique(df$Letter)))
scale_y_continuous(labels = function(x) scales::comma(abs(x)),
breaks = scales::pretty_breaks(10))
coord_flip()

uj5u.com熱心網友回復:
嘗試按值型別 (>0,<0) 拆分 geom_text。見下文:
df %>%
pivot_longer(-c(Letter)) %>%
mutate(value=ifelse(name=='X',value*-1,value)) %>%
ggplot(aes(x=Letter,y=value,fill=name))
geom_bar(stat = 'identity',color='black',alpha=0.7)
geom_text(data = . %>% filter(value>0), aes(label=format(abs(value),big.mark = '.')),
size=3,fontface='bold',hjust=0)
geom_text(data = . %>% filter(value<0), aes(label=format(abs(value),big.mark = '.')),
size=3,fontface='bold',hjust= 1)
scale_x_discrete(limits = rev(unique(df$Letter)))
scale_y_continuous(labels = function(x) scales::comma(abs(x)),
breaks = scales::pretty_breaks(10))
coord_flip()

uj5u.com熱心網友回復:
這是另一種方法,帶有ifelse宣告:
library(tidyverse)
#Plot
df1 <- df %>%
pivot_longer(-c(Letter)) %>%
mutate(value=ifelse(name=='X',value*-1,value))
ggplot(df1, aes(x=Letter,y=value,fill=name))
geom_bar(stat = 'identity',color='black',alpha=0.7)
geom_text(aes(label = format(value, big.mark = '.'),
vjust = ifelse(value >= 0, 0.5, 0.5),
hjust = ifelse(value>= 0, -0.05, 1.05)),
size=3,fontface='bold')
scale_x_discrete(limits = rev(unique(df$Letter)))
scale_y_continuous(labels = function(x) scales::comma(abs(x)),
breaks = scales::pretty_breaks(10))
coord_flip()

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315882.html
上一篇:在R中著色直方圖
