I have pie chart below in R.
I wan to :
1- change the font family to let s say Arial Narrow.
2- move the inner chart percentage labels farther from the center.
How can I do this?
ALSO, if possible I would love to know what s the logic behind the order R opts for legend? why not from the largest proportion to the smallest?
mydf <- data.frame(
causes=c("a", "b", "c", "d", "other"),
freq=c(100, 80, 78, 75, 120),#
share= 100*c(100/sum(freq), 80/sum(freq), 78/sum(freq), 75/sum(freq), 120/sum(freq))
)
sum(freq)
ggplot(mydf, aes("", share, fill = causes))
geom_bar(width = 1, size = 1, color = "white", stat = "identity")
coord_polar("y")
geom_text(aes(label = paste0(round(share), "%")),
position = position_stack(vjust = 0.5), size=5, col="white")
labs(x = NULL, y = NULL, fill = NULL,
title = "leading causes of death in US")
guides(fill = guide_legend(reverse = TRUE))
scale_fill_manual(values = c("#003B6D", "#6699CC", "#BDBDBD", "#676767", "#EBEDF3"))
theme_classic()
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, color = "#666666"))
CodePudding user response:
Try this way
mydf$Label <- round(((mydf$freq/sum(mydf$freq))*100),2)
mydf <- mydf %>%
mutate(end = 2 * pi * cumsum(Label)/sum(Label),
start = lag(end, default = 0),
middle = 0.5 * (start end),
hjust = ifelse(middle > pi, 1, 0),
vjust = ifelse(middle < pi/2 | middle > 3 * pi/2, 0, 1))
ggplot(mydf)
ggforce::geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = 1,
start = start, end = end, fill = causes))
geom_text(aes(x = 1.05 * sin(middle), y = 1.05 * cos(middle), label = Label,
hjust = hjust, vjust = vjust))
coord_fixed()
labs(x = NULL, y = NULL, fill = NULL,
title = "leading causes of death in US")
guides(fill = guide_legend(reverse = TRUE))
scale_fill_manual(values = c("#003B6D", "#6699CC", "#BDBDBD", "#676767", "#EBEDF3"))
theme_classic()
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, color = "#666666"))
Inside
mydf <- mydf %>%
mutate(end = 2 * pi * cumsum(Label)/sum(Label),
start = lag(end, default = 0),
middle = 0.5 * (start end),
hjust = ifelse(middle > pi, 0, 1),
vjust = ifelse(middle < pi/2 | middle > 3 * pi/2, 1, 0))
ggplot(mydf)
ggforce::geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = 1,
start = start, end = end, fill = causes))
geom_text(aes(x = 0.95 * sin(middle), y = 0.95 * cos(middle), label = Label,
hjust = hjust, vjust = vjust))
coord_fixed()
labs(x = NULL, y = NULL, fill = NULL,
title = "leading causes of death in US")
guides(fill = guide_legend(reverse = TRUE))
scale_fill_manual(values = c("#003B6D", "#6699CC", "#BDBDBD", "#676767", "#EBEDF3"))
theme_classic()
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, color = "#666666"))
font
ggplot(mydf)
ggforce::geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = 1,
start = start, end = end, fill = causes))
geom_text(aes(x = 0.95 * sin(middle), y = 0.95 * cos(middle), label = Label,
hjust = hjust, vjust = vjust), fontface = "bold") #inside chart here
coord_fixed()
labs(x = NULL, y = NULL, fill = NULL,
title = "leading causes of death in US")
guides(fill = guide_legend(reverse = TRUE))
scale_fill_manual(values = c("#003B6D", "#6699CC", "#BDBDBD", "#676767", "#EBEDF3"))
theme_classic()
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, color = "#666666"),
text = element_text(family = "sans")) #title here
for fontface
, "plain", "bold", "italic", "bold.italic" are available and for element_text(family)
, mono
sans
serif
Courier
Helvetica
Times
AvantGarde
Bookman
Helvetica-Narrow
NewCenturySchoolbook
Palatino
URWGothic
URWBookman
NimbusMon
URWHelvetica
NimbusSan
NimbusSanCond
CenturySch
URWPalladio
URWTimes
NimbusRom
are available.