英文:
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.
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 <- 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 <- 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 <- 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, 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.
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))
英文:
You can use parse = TRUE
in geom_text
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论