Reputation: 331
I have some data in I want to do a pie chart from it. In the data, there is a category that is to low and its label on the chart does not look good. My code and the respective plot are the following.
library("readr")
library("fmsb")
library("car")
library("normtest")
library("nortest")
library("moments")
library("readxl")
library("ggplot2")
library("stats")
library("data.table")
library("dplyr")
library("plotly")
count.data <- data.frame(
Tópico = c("Problemas de ruteo", "Diseño de redes ", "Ruteo y localización", "Modelos de inventario","Problemas de localización","Problemas de asignación","Medición de riesgo"),
n = c(27,6,16,10,31,6,4),
prop = c(27,6,16,10,31,6,4)
)
count.data
count.data <- count.data %>%
arrange(desc(Tópico)) %>%
mutate(lab.ypos = cumsum(prop) - 0.5*prop)
count.data
mycols <- c("#A1E89D", "#EFC000FF", "#868686FF", "#CD534CFF","#7AD55D","#9333FF","#FF33CE")
ggplot(count.data, aes(x = "", y = prop, fill =Tópico)) +
geom_bar(width = 1.8, stat = "identity", color = "black") +
coord_polar("y", start = 0)+
geom_text(aes(y = lab.ypos, label = paste0(prop, "%")), color = "black",cex=3.5)+
scale_fill_manual(values = mycols)+ theme(axis.text.x=element_blank())+theme_void()
The plot is:
As you can see, the category in yellow does not look good, in general, I would like to change the style of these labels, for example, this way:
Of course, I would like to preserve the legends, any ideas?. Thanks in advance
Upvotes: 3
Views: 516
Reputation: 494
A simple solution using Plotly.
plot_ly(count.data, labels = ~count.data$n, values = ~count.data$n, type = 'pie',
textposition = 'outside',
textinfo = 'label',
insidetextfont = list(color = '#FFFFFF'),
hoverinfo = 'text',
marker = list(line = list(color = '#FFFFFF', width = 1)),showlegend = TRUE)
Upvotes: 1