Reputation:
I currently have trouble plotting a 3D pie chart whereas it had worked well with a very similar dataset.
Here is my dataset :
structure(list(type_de_sejour = structure(1:4, levels = c("Ambulatoires",
"Externes", "Fictifs", "Hospitalisé"), class = "factor"), nb_sejours_2021 = c(20794,
365, 0, 7866)), row.names = c(NA, -4L), class = "data.frame")
And here is my code :
lab <- paste0(round(pie1_PGS$nb_sejours_2021/sum(pie1_PGS$nb_sejours_2021) * 100, 2),
"%")
pie3D(pie1_PGS$nb_sejours_2021, radius = 0.75,
height = 0.1,
theta = 0.7,
border = "white",
col = rainbow(length(lab)),
shade = 0.5,
labels = lab,
labelcol = "red",
labelcex = 0.75,
explode = 0.2,
main = "Répartition des séjours 2021 par type")
I get the following message of error :
"Error in seq.default(start, end, by = angleinc) :
(to - from) / by incorrect"
Also, I would like to plot a legend to indicate what the colours mean (they take the values of variable type_de_sejour). I have seen several posts here, but I can't seem to manage to do it on this dataset, so I would welcome any help regarding this issue too. Here is the code I added :
legend(0.5, 1.5, c("Ambulatoires","Externes", "Hospitalisé",
"Séances"), cex = 0.3,
fill = rainbow(length(lab)))
I think the problem is that the legend is too big as regards the plot...
Upvotes: 1
Views: 206
Reputation: 41603
Add legend
You can use the function legend
like this:
pie1_PGS <- structure(list(type_de_sejour = structure(1:4, levels = c("Ambulatoires",
"Externes", "Fictifs", "Hospitalisé"), class = "factor"), nb_sejours_2021 = c(20794,
365, 0, 7866)), row.names = c(NA, -4L), class = "data.frame")
pie1_PGS <- pie1_PGS[!(pie1_PGS$nb_sejours_2021 == 0),]
lab <- paste0(round(pie1_PGS$nb_sejours_2021/sum(pie1_PGS$nb_sejours_2021) * 100, 2),
"%")
library(plotrix)
pie3D(pie1_PGS$nb_sejours_2021,
radius = 0.75,
height = 0.1,
theta = 0.7,
border = "white",
col = rainbow(length(lab)),
shade = 0.5,
labels = lab,
labelcol = "red",
labelcex = 0.75,
explode = 0.2,
main = "Répartition des séjours 2021 par type")
legend(0.1, 0.9, pie1_PGS$type_de_sejour, cex = 0.7, fill = rainbow(length(lab)))
Created on 2022-08-11 by the reprex package (v2.0.1)
You should remove the rows with 0 value because you can't show them in a pie chart. You can use the following code:
pie1_PGS <- structure(list(type_de_sejour = structure(1:4, levels = c("Ambulatoires",
"Externes", "Fictifs", "Hospitalisé"), class = "factor"), nb_sejours_2021 = c(20794,
365, 0, 7866)), row.names = c(NA, -4L), class = "data.frame")
pie1_PGS <- pie1_PGS[!(pie1_PGS$nb_sejours_2021 == 0),]
lab <- paste0(round(pie1_PGS$nb_sejours_2021/sum(pie1_PGS$nb_sejours_2021) * 100, 2),
"%")
library(plotrix)
pie3D(pie1_PGS$nb_sejours_2021,
radius = 0.75,
height = 0.1,
theta = 0.7,
border = "white",
col = rainbow(length(lab)),
shade = 0.5,
labels = lab,
labelcol = "red",
labelcex = 0.75,
explode = 0.2,
main = "Répartition des séjours 2021 par type")
Created on 2022-08-10 by the reprex package (v2.0.1)
Upvotes: 1