I am trying to plot Donut plot with ggplot 2. Below you can see my data and also my plot.
test_data<-structure(list(KindOfParticipants = c("Participants", "Non-participants",
"Unknown group"), variable = structure(c(1L, 1L, 1L), .Label = "Total", class = "factor"),
value = c(111L, 5937L, 18L), fraction = c(0.0431091877496671,
0.953894806924101, 0.00299600532623169), ymax = c(0.0431091877496671,
0.997003994673768, 1), ymin = c(0, 0.0431091877496671, 0.997003994673768
), labelPosition = c(0.0215545938748336, 0.520056591211718,
0.998501997336884), label = c("Participants\n value: 111", "Non-Participants\n value: 5937",
"Unknown group\n value: 110")), row.names = c(NA, -3L), class = "data.frame")
Now I want to plot Donut plot with this data.
plot_1_test<-ggplot(test_data, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=KindOfParticipants))
geom_rect()
geom_text( x=1, aes(y=labelPosition, label=label, color=variable), size=6) # x here controls label position (inner / outer)
scale_fill_brewer(palette=3)
scale_color_brewer(palette=3)
coord_polar(theta="y")
xlim(c(-1, 4))
theme_void()
theme(legend.position = "none")
ggtitle("Structure of participants or non participants or unkown groups")
plot_1_test
But these lines of code give me a Donut plot like the plot below :
So with this plot, I have two problems connected mainly with text inside the circle
First I want to have readable text inside the circle, now there is overlapping of text and second to have darker color inside the circle.
Can anybody help me with how to solve this problem?
CodePudding user response:
You can adjust your label with hjust
like below.
require(ggplot2)
plot_1_test <- ggplot(test_data, aes(ymax=ymax, ymin=ymin,
xmax=4, xmin=3,
fill=KindOfParticipants))
geom_rect()
# geom_label(x=3.5,aes(y=labelPosition, label=label),hjust=c(0,1,2))
geom_text(x=1, aes(y=labelPosition, label=label,
color=KindOfParticipants),
size=5,
hjust=c(-0.2,0.5,1)) # x here controls label position (inner / outer)
scale_fill_brewer(palette=3)
scale_color_brewer(palette=3)
coord_polar(theta="y")
xlim(c(-1, 4))
theme_void()
theme(legend.position = "none")
ggtitle("Structure of participants or non participants or unkown groups")
plot_1_test
Created on 2022-05-03 by the reprex package (v2.0.1)