在R中使用ggplot2的geom_text()函数来使用变量设置斜体文本。

huangapple go评论64阅读模式
英文:

Using italics in geom_text() with variables in R with ggplot2

问题

I have some troubles with geom_text() and annotate(), respectively. First of all, I want to show the number of observations of my dot plot/barplot within the plot. I have now solved it awkwardly via an additional vector in the dataframe where the number is stored.

library(dplyr)
library(ggplot2)
v1 <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
v2 <- c(0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
v3 <- c(13,67,89,280,40,1,23,99,32,1,75,280,270,200,196,450,320,277,23,4,1,2,5,89,45,23,11,1,3,23,100,100,100,100,100,200,100,11,6,6,123,100,100,100,100,100,12,86,11,390,75,100,110,19,299,100,100,100,100,100,100,100,100,11,100,120,110,100,100,423,100,279,100,100,100,12,100,100,75,5,10,10,10,10,10)
v4 <- c(4,NA,NA,NA,5,NA,NA,NA,NA,NA,9,NA,NA,NA,NA,NA,NA,NA,NA,10,NA,NA,NA,NA,NA,NA,NA,NA,NA,22,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,34,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA)

summary <- data.frame(v1, v2, v3, v4)
summary$v1 <- as.factor(summary$v1)
summary$v2 <- as.factor(summary$v2)

ggplot(summary, aes(x = v1, y = v3, fill = v2)) +
    geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 5, position = position_dodge(0.75), dotsize = 1) +
    geom_text(aes(label = ifelse(v4 > 0, paste("n=", v4), NULL), y = 500), position = position_dodge(width = 0.75), vjust = -0.25, hjust = 0)

Now I have a problem:

I would like to insert an "n" in italics before the number of observations (v4). I have done a lot of searching on this as well. One solution I found was substitute(paste0(italic('n='), summary$v4)), but here I get the following error:
Error in rep(yes, length.out = len) : attempt to replicate an object of type 'language'.

If I use the following code: expression(paste0(italic('n='), summary$v4))
The "n=" is displayed in italic, but the variable is treated as text and the content of the variable is not displayed.

here an example image

Probably there is also an automatic solution how to count the number of observations automatically. Also here I would be very happy about improvements and help.

英文:

I have some troubles with geom_text() and annotate(), respectively. First of all, I want to show the number of observations of my dot plot/barplot within the plot.
I have now solved it awkwardly via an additional vector in the dataframe where the number is stored.

library(dplyr)
library(ggplot2)  
v1 &lt;- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
v2 &lt;- c(0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
v3 &lt;- c(13,67,89,280,40,1,23,99,32,1,75,280,270,200,196,450,320,277,23,4,1,2,5,89,45,23,11,1,3,23,100,100,100,100,100,200,100,11,6,6,123,100,100,100,100,100,12,86,11,390,75,100,110,19,299,100,100,100,100,100,100,100,100,11,100,120,110,100,100,423,100,279,100,100,100,12,100,100,75,5,10,10,10,10,10)
v4 &lt;- c(4,NA,NA,NA,5,NA,NA,NA,NA,NA,9,NA,NA,NA,NA,NA,NA,NA,NA,10,NA,NA,NA,NA,NA,NA,NA,NA,NA,22,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,34,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA)
summary &lt;- data.frame(v1, v2, v3, v4)
summary$v1 &lt;- as.factor(summary$v1)
summary$v2 &lt;- as.factor(summary$v2)
ggplot(summary, aes(x = v1, y = v3, fill = v2)) + 
geom_dotplot(binaxis  = &quot;y&quot;, stackdir = &quot;center&quot;, binwidth = 5, position = position_dodge(0.75), dotsize = 1) +
geom_text(aes(label = ifelse(v4&gt;0, v4, NULL), y=500), position=position_dodge(width=0.75), vjust=-0.25, hjust=0)

Now I have a problem:

I would like to insert an "n" in italics before the number of observations (v4). I have done a lot of searching on this as well. One solution I found was substitute(paste0(italic(&#39;n=&#39;), summary$v4)), but here I get the following error:
Error in rep(yes, length.out = len) :
attempt to replicate an object of type &#39;language&#39;.

If I use the following code: expression(paste0(italic(&#39;n=&#39;), summary$v4))
The "n=" is displayed in italic, but the variable is treated as text and the content of the variable is not displayed.

here an example image

Probably there is also an automatic solution how to count the number of observations automatically. Also here I would be very happy about improvements and help.

答案1

得分: 1

你可以在geom_text中使用parse = TRUE

ggplot(summary, aes(x = v1, y = v3, fill = v2)) + 
  geom_dotplot(binaxis  = "y", stackdir = "center", binwidth = 5, 
               position = position_dodge(0.75), dotsize = 1) +
  geom_text(aes(label = ifelse(!is.na(v4), paste0('italic(n) == ', v4), ''), 
                y = 500), vjust = -0.25, hjust = 0, parse = TRUE,
            position = position_dodge(width = 0.75))

在R中使用ggplot2的geom_text()函数来使用变量设置斜体文本。

英文:

You can use parse = TRUE in geom_text

ggplot(summary, aes(x = v1, y = v3, fill = v2)) + 
  geom_dotplot(binaxis  = &quot;y&quot;, stackdir = &quot;center&quot;, binwidth = 5, 
               position = position_dodge(0.75), dotsize = 1) +
  geom_text(aes(label = ifelse(!is.na(v4), paste0(&#39;italic(n) == &#39;, v4), &#39;&#39;), 
                y = 500), vjust = -0.25, hjust = 0, parse = TRUE,
            position = position_dodge(width = 0.75))

在R中使用ggplot2的geom_text()函数来使用变量设置斜体文本。

huangapple
  • 本文由 发表于 2023年5月6日 21:42:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定