簡體   English   中英

geom_bar在geom_bar中的絕對位置(ggplot2)

[英]Absolute position of geom_text in a geom_bar (ggplot2)

我想在一個條形圖中插入一些文字。 我設法,但定位很奇怪,好像它不是從相同的絕對y值開始。

data(iris)

ggplot(iris, aes(x = Species)) +
  geom_bar(aes(y = Sepal.Length, fill = "pink"), stat = "identity") +
  geom_text(aes(y=1, label = Species, hjust = -2, angle = 90))

在此輸入圖像描述

我希望文本從相同的y值開始。

除了hjust問題,您還有其他兩個問題與您的代碼:(1)你必須把fill = "pink"的外部aes和(2)的geom_text地塊的所有文字標簽為每個組在彼此的頂部,其可能會導致標簽不那么尖銳。

您可以按如下方式解決此問題:

# create a separate labals dataframe
iris.lbl <- data.frame(lbl = levels(iris$Species), y=100)

# make the plot with everything set correctly
ggplot(iris, aes(x = Species)) +
  geom_bar(aes(y = Sepal.Length), stat = "identity", fill = "pink") +
  geom_text(data = iris.lbl, aes(x=lbl, y=y, label = lbl, hjust=0, angle = 90))

這使:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM