簡體   English   中英

Plot 顯示兩個相同的項目 label (兩行,只有一個標簽)ggplot R facet

[英]Plot display two items with same label (two lines, only one label) ggplot R facet

我想有兩條不同的行,其值為 object,名稱相同,但在其他方面有所不同。 它應該只有一行文本。

library(tidyverse)
library(data.table)
#gen some example code
c <- c('a', 'a', 'b', 'b')
d <- c('firstfirst', 'firstfirst', 'lowerupper', 'lowerlower')
e <- c(0.2, 0.3, 0.4, 0.5)
f <- c('w', 'v','w', 'v')
df <- cbind(c,d,e,f)
df<- as.data.frame(df)
df$e <- as.numeric(df$e)
orderd <- c( 'firstfirst', 'firstfirst', 'lowerupper', 'lowerlower' )
df<- within(df, d <- factor(d, levels=orderd))


ggplot(df, aes(x = d, y = e, color = f)) +
  geom_pointrange(aes(min = e - 1.95 * sqrt(e), max = e + 1.95 * sqrt(e)), shape = 15) +
  theme_bw() + 
  facet_wrap(c ~ .,  nrow = 5, strip.position = "left") +
  coord_flip() +
  scale_colour_viridis_d(begin = 0.75 , end = 0) +
  geom_text(aes(label = f), colour = "black", size = 2.5, hjust=1.05, vjust=1.2)

在圖表中,我希望在兩個面板上只有一條線表示“firstfirst”,但我想要兩條線,一條用於 f = v,另一條用於 f=w。

我目前的工作是 label 其中一個“firstfirst”(即有一個額外的空格)。 但是,這只會產生兩行,請參見此處的示例:

怎么可能只有一次文本但有兩行?

如果對於這種類型的多個圖很容易重現,則加分!

圖片顯示了我想要的(在 R 之外編輯)。 圖片顯示了我想要的(在 R 之外編輯)

最簡單的可能是使用 position_dodge。 以您的示例代碼為例,您可以簡單地將 position_dodge 命令添加到 geom_pointrange

 geom_pointrange(aes(min = e - 1.95 * sqrt(e), max = e + 1.95 * sqrt(e)), shape = 15, position = position_dodge(width=1)) 

導致這張照片

暫無
暫無

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

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