Customizing text (text size, text color, text angle) in Plotly for R

隐身守侯 提交于 2019-12-13 03:29:39

问题


I'm working on making a donut chart/open pie chart in Plotly. Because of help from this question, Open Pie Chart/Donut Chart in R using Plotly with count and percentage, I was able to get fairly far with my plot. However, now I need help customizing the text size, angle, and color.

Here's the dataset:

library(dplyr)
testfile <- tibble(personID = 1:10,
                   status = c("bad", "good", "bad", "bad", "bad", "bad", "bad", "bad", "bad", "good"),
                   department = c("sales", "sales", "marketing", "sales", "marketing", "management", "management", "sales", "sales", "sales"))

Here's the code to make the plot thus far. It has the count and percentage for each status, and in the center, it has the percentage of "good" status individuals.

library(plotly)

plot <- plot_ly(values, labels = ~status, values = ~count, text = ~count, color = I("white")) %>%
  add_pie(hole = 0.6) %>%
  layout(title = "Ratio of Good to Bad",  showlegend = F, position = "topcenter", 
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE))

plot <- layout(p, annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F))
plot

This code yields these two plots. Surprisingly, it looks the first way when I click "zoom" on R Studio, but it looks the second (very wrong) way when I click export and open the PNG file.

Zoomed plot (mostly correct)

Exported plot (looks very different than how it should...)

This zoomed in plot is exactly what I want except for three issues:

  1. I want the center text (20%) to be larger. I've tried p <- layout(p, annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F, size = 25)) but that did nothing.

  2. I want both of the labels (2 20% and 8 80%) to be white, not just the 8 80%. I thought color = I("white") would do the trick, but the output is the same as when I don't specify the color at all.

  3. I want the labels to be horizontal instead of curving and for them to fit within the bars.

Optional: Why does it look different when it is exported vs zoomed in? I need it to look like the zoomed in version. Also, is there also an easy way to get it to say n = 2 instead of just 2?

Thank you!

EDIT: This code makes the labels horizontal, but they are too small. When I add size = 15 or any number, it always makes the font the same large size but no longer horizontal. This also fixes the issue where the export and zoom are inconsistent. It also fixes the center text size. I just need the labels to both be white, larger in size, and horizontal.

plot <- plot_ly(values, labels = ~status, values = ~count, text = ~count) %>%
  add_pie(hole = 0.6) %>%
  layout(title = "Ratio of Good to Bad",  showlegend = F, position = "topcenter", 
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
         annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F, font=list(size = 40)))
plot


回答1:


Update, I cracked the code! Here's the code that worked

library(plotly)
library(dplyr)

values <- testfile %>%
  group_by(status) %>%
  summarize(count = n())

t <- list(size = 14, color = "white")

good <- values %>% filter(status == 'good')

plot <- plot_ly(values, labels = ~status, values = ~count, text = ~count, textfont = list(color = "white", size = 40)) %>%
  add_pie(hole = 0.6) %>%
  layout(title = "Ratio of Good to Bad",  showlegend = TRUE, 
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = TRUE),
         annotations=list(text=paste(good$count / sum(values$count) * 100, "%", sep=""), "showarrow"=F, font=list(size = 180, color = "black")))
plot

Here's what the plot looks like now:



来源:https://stackoverflow.com/questions/56532527/customizing-text-text-size-text-color-text-angle-in-plotly-for-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!