1

The text printed using geom_text in my circular bar plot is not acceptable and the resolution is very low. Given the label is too long, I wanted to fold them up to make it short. However, I did not find any means to do so. How can I make it?

 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 )

enter image description here enter image description here

Tung
  • 26,371
  • 7
  • 91
  • 115
scott Yang
  • 21
  • 3
  • 1
    Your code is not working. Please make sure it runs in a fresh R session. You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Feb 07 '20 at 02:55
  • Check your code, you wrote `df1` in the ggplot command instead of `df2`. – dc37 Feb 07 '20 at 03:05
  • 2
    You might try the `ragg` package, which handles rotated text well: https://ragg.r-lib.org/articles/ragg_quality.html – Jon Spring Feb 07 '20 at 04:21

1 Answers1

1

You can either create a new column in your "label_data" dataframe to introduce some \n into long sequence names such as:

Label_data2 <- label_data %>% rowwise() %>%
  mutate(DrugID = as.character(DrugID)) %>%
  mutate(Short = ifelse(nchar(DrugID) > 15, paste0(substr(DrugID,1,15),"\n",substr(DrugID,16,nchar(DrugID)))
                                              , DrugID))

And then plot it using this new label dataframe

ggplot(df2, aes(x=as.factor(No), y= Score)) +
  geom_bar(stat = "identity", fill = "skyblue", alpha = 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_data2, aes(x=No, y=Score+0.1, label= Short, 
                                   hjust=hjust, angle = angle), color = "black", fontface=2, 
            alpha=2.0, size=2.5,inherit.aes = FALSE, show.legend = TRUE)

enter image description here

Alternatively, you can use the function abbreviate:

ggplot(df2, aes(x=as.factor(No), y= Score)) +
  geom_bar(stat = "identity", alpha = 0.4, fill = "skyblue") +
  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= abbreviate(DrugID, minlength = 12), 
                                   hjust=hjust, angle = angle), color = "black", fontface=2, 
            alpha=2.0, size=2.5,inherit.aes = FALSE, show.legend = TRUE)

enter image description here

Hope it answers your question

dc37
  • 15,840
  • 4
  • 15
  • 32
  • Thank you for the advice! I managed to get a nice looking bar graph with the "tiff" and "dev.off" functions. – scott Yang Feb 07 '20 at 04:08