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...
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)
Related
I have a problem with the legend on a pie chart, I already received help from here but it still doesn't work as I hoped (i don't have the entire legend on there, the frame of the legend is partly outside it) and I don't know why. Here is what I get when running the following code :
Here is my code :
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.5, 1.1, pie1_PGS$type_de_sejour, cex = 0.3, fill = rainbow(length(lab)))
Here is my dataset :
structure(list(type_de_sejour = structure(c(1L, 2L, 4L), levels = c("Ambulatoires",
"Externes", "Fictifs", "Hospitalisé"), class = "factor"), nb_sejours_2021 = c(20794,
365, 7866)), row.names = c(1L, 2L, 4L), class = "data.frame")
Could anyone help ?
You could use "topright" and the argument inset to inset distance from the margin like this:
pie1_PGS <- structure(list(type_de_sejour = structure(c(1L, 2L, 4L), levels = c("Ambulatoires",
"Externes", "Fictifs", "Hospitalisé"), class = "factor"), nb_sejours_2021 = c(20794,
365, 7866)), row.names = c(1L, 2L, 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("topright", legend = pie1_PGS$type_de_sejour, cex = 0.3, fill = rainbow(length(lab)), inset = 0.05)
Created on 2022-08-12 by the reprex package (v2.0.1)
This is my dataframe:
df<-structure(list(year = c(1984, 1984), team = c("Australia", "Brazil"
), continent = c("Oceania", "Americas"), medal = structure(c(3L,
3L), .Label = c("Bronze", "Silver", "Gold"), class = "factor"),
n = c(84L, 12L)), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"))
And this is my ggplot (my question is related to the annotations regard Brazil label):
ggplot(data = df)+
geom_point(aes(x = year, y = n)) +
geom_text_repel(aes(x = year, y = n, label = team),
size = 3, color = 'black',
seed = 10,
nudge_x = -.029,
nudge_y = 35,
segment.size = .65,
segment.curvature = -1,
segment.angle = 178.975,
segment.ncp = 1)+
coord_flip()
So, I have a segment divided by two parts. On both parts I have 'small braks'. How can I avoid them?
I already tried to use segment.ncp, change nudge_xor nudge_ynut its not working.
Any help?
Not really sure what is going on here. This is the best I could generate by experimenting with variations to the input values for segment... arguments.
There is some guidance at: https://ggrepel.slowkow.com/articles/examples.html which has an example with shorter leader lines, maybe that's an approach you could use.
df<-structure(list(year = c(1984, 1984), team = c("Australia", "Brazil"
), continent = c("Oceania", "Americas"), medal = structure(c(3L,
3L), .Label = c("Bronze", "Silver", "Gold"), class = "factor"),
n = c(84L, 12L)), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"))
library(ggplot2)
library(ggrepel)
ggplot(data = df)+
geom_point(aes(x = year, y = n)) +
geom_text_repel(aes(x = year, y = n, label = team),
size = 3, color = 'black',
seed = 1,
nudge_x = -0.029,
nudge_y = 35,
segment.size = 0.5,
segment.curvature = -0.0000002,
segment.angle = 1,
segment.ncp = 1000)+
coord_flip()
Created on 2021-08-26 by the reprex package (v2.0.0)
I need to create a forestplot of high resolution. I used the forestplot() function from library(forestplot) to create my plot, and then attempted to use the tiff() function to create a high resolution image for publication. However, my image turned blank.
It works if I export directly from R but not as high resolution as it was supposed to.
library(forestplot)
df <- structure(list(
mean = c(NA, 0.22, 0.20, 0.27),
lower = c(NA, 0.05, 0.04, 0.01),
upper = c(NA, 0.95, 1.08, 9.12)),
.Names = c("mean", "lower", "upper"),
row.names = c(NA, -4L),
class = "data.frame")
tabletext <- cbind(
c("", "Pooled", "Group 1", "Group 2"),
c("N", "4334", "3354", "980"),
c("HR (95% CI)", "0.22 (0.05, 0.95)", "0.20 (0.04, 1.08)", "0.27 (0.01, 9.12)"),
c("p-value", "0.042", "0.061", "0.467")
)
ggfp <- forestplot(tabletext,
df,
new_page = TRUE,
is.summary = c(TRUE, rep(FALSE, 3)),
clip = c(0, 2),
colgap = unit(5, "mm"),
line.margin = unit(2, "mm"),
lineheight = unit(1, "in"),
txt_gp = fpTxtGp(label = gpar(cex = 1),
ticks = gpar(cex = 1)),
align = c("l", "c", "c", "c"),
boxsize = 0.2,
xticks = seq(0, 2.0, 0.5),
zero = 1,
col = fpColors(box = "royalblue",
line = "darkblue"),
mar = unit(c(-1, 0.5, -2, 0.5), "in"))
tiff("forestplot.tiff", units = "in", width = 9, height = 7, res = 300)
ggfp
dev.off()
The file was created but it was a blank page
This works for me (output file is 17MB):
library(forestplot)
setwd("/path/to/directory/for/plot")
df <- structure(list(
mean = c(NA, 0.22, 0.20, 0.27),
lower = c(NA, 0.05, 0.04, 0.01),
upper = c(NA, 0.95, 1.08, 9.12)),
.Names = c("mean", "lower", "upper"),
row.names = c(NA, -4L),
class = "data.frame")
tabletext <- cbind(
c("", "Pooled", "Group 1", "Group 2"),
c("N", "4334", "3354", "980"),
c("HR (95% CI)", "0.22 (0.05, 0.95)", "0.20 (0.04, 1.08)", "0.27 (0.01, 9.12)"),
c("p-value", "0.042", "0.061", "0.467")
)
tiff("forestplot.tiff", units = "in", width = 9, height = 7, res = 300)
forestplot(tabletext,
df,
new_page = TRUE,
is.summary = c(TRUE, rep(FALSE, 3)),
clip = c(0, 2),
colgap = unit(5, "mm"),
line.margin = unit(2, "mm"),
lineheight = unit(1, "in"),
txt_gp = fpTxtGp(label = gpar(cex = 1),
ticks = gpar(cex = 1)),
align = c("l", "c", "c", "c"),
boxsize = 0.2,
xticks = seq(0, 2.0, 0.5),
zero = 1,
col = fpColors(box = "royalblue",
line = "darkblue"),
mar = unit(c(-1, 0.5, -2, 0.5), "in"))
dev.off()
I'd like to distinguish between statistical significance (OR = 1.0) and clinical significance (OR = 1.5) in my forest plot. I created this plot using the forestplot package, sample code below. Is adding a vertical line possible (while maintaining the line of no difference)?
library(forestplot)
test_data <- structure(list(
mean = c(NA, NA, 1, 0.5, 2),
lower = c(NA, NA, .5, .25, 1.5),
upper = c(NA, NA, 1.5, .75, 2.5)),
.Names = c("mean", "lower", "upper"),
row.names = c(NA, -5L),
class = "data.frame")
tabletext <- cbind(
c("", "Outcome", "Outcome 1", "Outcome 2", "Outcome 3"),
c("", "OR", "1 (0.5 - 1.5)", "0.5 (0.25 - 0.75)", "2.0 (1.5 - 2.5)"))
forestplot(tabletext,
test_data,
new_page = TRUE,
xlog = TRUE,
boxsize = .25
)
Is this what you were looking for?
forestplot(tabletext,
test_data,
new_page = TRUE,
xlog = TRUE,
grid = structure(c(log(1.5)),
gp = gpar(lty = 2, col = "#CCCCFF")),
zero = 1,
boxsize = .25)
A suboptimal (and not very elegant) solution could be: 1- creating an empty plot with no axis or labels, 2- then plot a vertical line (abline(v=1.5)) and 3- call your forestplot with new_page = F.
I would assign a name for every circle in a Venn diagram. I have tried to change options in category but seems this is the only set I can use. I attach my code, please where is the wrong part?
goterm3 = c(1,2,3,4,5,6)
goterm2 =c(2,2,3,4,3,5)
goterm1=c(4,5,3,2,4,3,2,4)
int12 = intersect(goterm1, goterm2)
int13 = intersect(goterm1, goterm3)
int23 = intersect(goterm2, goterm3)
intall = intersect(int12, goterm3)
require(VennDiagram)
venn.plot = draw.triple.venn(length(goterm1), length(goterm2), length(goterm3),
length(int12), length(int23), length(int13),length(intall),
category = rep("ORG1, ORG2,Org",3) ,rotation = 1, reverse = FALSE, euler.d = FALSE,
scaled = FALSE, lwd = rep(2, 3), lty = rep("solid", 3),
col = rep("black", 3), fill = c("blue", "red", "green"),
alpha = rep(0.5, 3),
label.col = rep("black", 7), cex = rep(1, 7), fontface = rep("plain", 7),
fontfamily = rep("serif", 7), cat.pos = c(0, 0, 180),
cat.dist = c(0.05, 0.05, 0.025), cat.col = rep("black", 3),
cat.cex = rep(1, 3), cat.fontface = rep("plain", 3),
cat.fontfamily = rep("serif", 3),
cat.just = list(c(0.5, 1), c(0.5, 1), c(0.5, 0)), cat.default.pos = "outer",
cat.prompts = FALSE, rotation.degree = 0, rotation.centre = c(0.5, 0.5),
ind = TRUE, sep.dist = 0.05, offset = 0)
This is what I get and it does have the same labels as your categories (after I unmangled the string values for the categories:
category = c("ORG1", "ORG2","Org") # no rep needed and proper quotes