Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot:geom_文本输出的文本不清晰,字体很糟糕_R_Ggplot2 - Fatal编程技术网

R ggplot:geom_文本输出的文本不清晰,字体很糟糕

R ggplot:geom_文本输出的文本不清晰,字体很糟糕,r,ggplot2,R,Ggplot2,在我的圆形条形图中使用geom_文本打印的文本是不可接受的,并且分辨率非常低。由于标签太长,我想把它们折叠起来,使它短一些。然而,我没有找到任何办法这样做。我怎样才能做到 library(tidyverse) df2 <- data.frame (No=seq(1,6), DrugID = c('glyphosate','triclosan','alfacalcidol','tertracyline', "3,3',5,5

在我的圆形条形图中使用geom_文本打印的文本是不可接受的,并且分辨率非常低。由于标签太长,我想把它们折叠起来,使它短一些。然而,我没有找到任何办法这样做。我怎样才能做到

 library(tidyverse)
 df2 <- data.frame (No=seq(1,6),
 DrugID = 
 c('glyphosate','triclosan','alfacalcidol','tertracyline', 
                            "3,3',5,5'-Tetraiodothyroacetic Acid", 
 'Calcitriol'),
 Score = c('0.98','0.93','0.95','0.98','0.78','0.86'))

 label_data <- df2
 number_of_bar <- nrow(label_data)
 angle <- 90 - 360*(label_data$No-0.5) /number_of_bar
 label_data$hjust<-ifelse( angle < -90, 1, 0)
 label_data$angle <- ifelse(angle < -90, angle+180, angle)
 p <- ggplot(df1, aes(x=as.factor(No), y= Score)) +
 geom_bar(stat = "identity", fill=alpha("skyblue", 0.4)) +
 ylim(-1.0, 1.5) +
 theme_minimal() +
 theme(
 axis.text = element_blank(),
 axis.title = element_blank(),
 panel.grid = element_blank(),
 plot.margin = unit(rep(0,8), "cm")
 ) +
 coord_polar(start=0) +
 geom_text(data = label_data, aes(x=No, y=Score+0.1, label=DrugID, 
          hjust=hjust, vjust=-0.02), color = "black", fontface=2, 
          alpha=2.0, size=2.5, angle=label_data$angle, inherit.aes = FALSE) 
 +
 annotate("text", x=No, y=Score+0.1, label=DrugID, 
         hjust=hjust, vjust=-0.02, alpha=2.0, size=2.5 )
库(tidyverse)

df2您可以在“label\u data”数据框中创建一个新列,将一些
\n
引入长序列名,例如:

Label_data2%rowwise()%%>%
变异(DrugID=as.character(DrugID))%>%
突变(Short=ifelse(nchar(药物ID)>15,paste0(substr(药物ID,1,15),“\n”,substr(药物ID,16,nchar(药物ID)))
(药剂师)
然后使用这个新的标签数据框绘制它

ggplot(df2,aes(x=as.factor(No),y=Score))+
几何图形栏(stat=“identity”,fill=“skyblue”,alpha=0.4)+
ylim(-1.0,1.5)+
主题_极小值()+
主题(
axis.text=元素_blank(),
axis.title=元素_blank(),
panel.grid=element\u blank(),
plot.margin=单位(代表(0,8),“厘米”))+
坐标(起点=0)+
几何图形文本(数据=标签数据2,aes(x=否,y=分数+0.1,标签=短,
hjust=hjust,angle=angle),color=“black”,fontface=2,
alpha=2.0,size=2.5,inherit.aes=FALSE,show.legend=TRUE)

或者,您可以使用函数
缩写

ggplot(df2,aes(x=as.factor(No),y=Score))+
几何图形栏(stat=“identity”,alpha=0.4,fill=“skyblue”)+
ylim(-1.0,1.5)+
主题_极小值()+
主题(
axis.text=元素_blank(),
axis.title=元素_blank(),
panel.grid=element\u blank(),
plot.margin=单位(代表(0,8),“厘米”))+
坐标(起点=0)+
geom_文本(数据=标签数据,aes(x=否,y=分数+0.1,标签=缩写(DrugID,minlength=12),
hjust=hjust,angle=angle),color=“black”,fontface=2,
alpha=2.0,size=2.5,inherit.aes=FALSE,show.legend=TRUE)


希望它能回答您的问题

您的代码不起作用。请确保它在新的R会话中运行。您可以使用和包来帮助您实现这一点。另请参见并检查您的代码,您在ggplot命令中编写了
df1
,而不是
df2
。您可以尝试
ragg
软件包,它可以很好地处理旋转文本:谢谢您的建议!我设法得到了一个带有“tiff”和“dev.off”函数的好看的条形图。