Add pie chart in a bar graph - r

My goal is to create a bar graph of categorical variables and then add a pie chart inside it as shown in the attached image.
my data is the same as in the image and is below:
#For bar grapgh
chromosomes = c("1A", "1B", "1D", "2A", "2B", "2D", "3A", "3B", "3D", "4A","4B","4D","5A","5B","5D","6A","6B","6D","7A","7B","7D")
Frequency = c(668, 752, 213, 826, 948, 334, 625, 834, 264, 488, 391, 136, 745, 917, 234, 543, 848, 182, 901, 740, 241)
data_bar <- data.frame(chromosomes, Frequency)
#For pie chart
Genome = c("A","B","D")
Count = c(4796, 5430, 1604)
data_pie <- data.frame(Genome, Count)
I will highly appreciate if anyone can guide me or direct me towards where I can find the answers

Here's a ggplot2 solution:
pie <- ggplot(data_pie, aes(x = 1, y = Count, fill = Genome)) +
geom_col(color = "black") +
scale_fill_manual(values = c("red2", "forestgreen", "dodgerblue3")) +
coord_polar(theta = "y") +
theme_void() +
theme(legend.position = "none")
ggplot(data_bar) +
geom_col(aes(chromosomes, Frequency, fill = substr(chromosomes, 2, 2)),
color = "black", width = 0.5) +
scale_fill_manual(values = c("red2", "forestgreen", "dodgerblue3")) +
theme_classic() +
theme(legend.position = "none") +
annotation_custom(ggplotGrob(pie), xmin = "2B", xmax = "6A", ymin = 500)

Using only base R functions, please see the code below:
## Your data ---------------------------------
#For bar grapgh
chromosomes = c("1A", "1B", "1D", "2A", "2B", "2D", "3A", "3B", "3D", "4A","4B","4D","5A","5B","5D","6A","6B","6D","7A","7B","7D")
Frequency = c(668, 752, 213, 826, 948, 334, 625, 834, 264, 488, 391, 136, 745, 917, 234, 543, 848, 182, 901, 740, 241)
#For pie chart
Genome = c("A","B","D")
Count = c(4796, 5430, 1604)
## One idea to start with --------------------
plot.new()
par(mfrow = c(2, 1), # set the layout, you might also consider layout() or split.screen() as suggested in ?par
mar=c(4, 4, 1, 1), # this set up give enough space to see axis labels and titles
oma=c(0, 0, 0, 0)) # remove outer margins
pie(Count, labels = Genome)
barplot(Frequency~chromosomes)
Output:
I think it is possible to make it look cleaner adjusting par() arguments but I am not very familiar with them.
There are also packages cowplot and gridExtra that works nicely with ggplot2.

Related

How to plot significance in ggplot geom_bar() with multiple facets and groups?

I created a multi-facet plot with 4 groups of 3 bars each in every panel using ggplot2.
I then ran a separate test to see if there are statistically significant differences between each combination of the bar categories in each age group and in each facet, which produced a separate data.frame p.val.df. I now need to figure out how to get significance bars to appear between the bars, just like in this answer but additionally within each age group. This is where I am running aground. Since I already have my p-values, I don't need to calculate them using geom_signif() of the ggsignif package, but would rather just use geom_bracket of ggpubr package to plot them. But any way you could make it work is acceptable.
Here's the data and code:
library(ggplot2)
library(ggpubr)
# Main data
df <- data.frame(
factor(rep(c("A1", "A2"), each = 12), levels = c("A1", "A2")),
factor(rep(c("G", "M", "B"), each = 4), levels = c("G", "M", "B")),
factor(rep(c("0-2", "3-5", "6-12", "13-17"), 6), levels = c("0-2", "3-5", "6-12", "13-17")),
c(160, 162, 169, 108, 110, 111, 76, 73, 76, 45, 41, 38, 175,
177, 173, 174, 167, 172, 176, 162, 166, 143, 130, 143))
colnames(df) <- c("Class", "Type", "Age", "Coefficient")
# Intergroup difference significance
p.val.df <- data.frame(
factor(rep(c("A1", "A2"), each = 12), levels = c("A1", "A2")),
factor(rep(c("G", "M", "G"), each = 4), levels = c("G", "M")),
factor(rep(c("B", "B", "M"), each = 4), levels = c("B", "M")),
factor(rep(c("0-2", "3-5", "6-12", "13-17"), 6), levels = c("0-2", "3-5", "6-12", "13-17")),
c(0.635, 0.584, 0.268, 0.051, 0.163, 0.779, 0.302, 0.361, 0.055, 0.425, 0.998, 0.055,
0.707, 0.230, 0.000, 0.002, 0.418, 0.313, 0.211, 0.037, 0.675, 0.764, 0.011, 0.881))
colnames(p.val.df) <- c("Class", "Type1", "Type2", "Age", "p.value")
# Plotting
ggplot(df, aes(x = Age, y = Coefficient, fill = Type)) +
geom_bar(position = "dodge", stat = "identity") +
labs(y = "Coefficient", x = "Age", fill = "Type") +
facet_wrap( ~ Class, scales = "free") +
expand_limits(y = 300) +
theme_classic() +
### NOT SURE HOW TO PROCEED HERE
geom_bracket(
data = p.val.df, y.position = 250, step.increase = 0.1,
aes(xmin = Type1, xmax = Type2, label = signif(p.value, 2)))

Customizing Euler Diagram colors with eulerr R

I'm trying to plot an Euler Diagram using the eulerr package by Johan Larsson in R. I'm following this example where the developer explains how to customize colors/border transparency. However, when I try to implement it with the following code:
fit2 <- euler(c(A = 16971, B = 218, C = 215,
"A&B" = 112, "A&C" = 112, "B&C"= 51,"A&B&C" = 23))
plot(fit2,
polygon_args = list(col = c("dodgerblue4", "darkgoldenrod1", "cornsilk4"),
border = "transparent"),
text_args = list(font = 8), counts=TRUE)
Colors/border remain unchanged.
I am using RStudio 1.0.136, R 3.3.1 on Windows.
Sorry about this. That post is old and the arguments have changed.
Try this instead
fit2 <- euler(c(A = 16971, B = 218, C = 215,
"A&B" = 112, "A&C" = 112, "B&C"= 51,"A&B&C" = 23))
plot(fit2,
fills = c("dodgerblue4", "darkgoldenrod1", "cornsilk4"),
edges = FALSE,
fontsize = 8,
quantities = list(fontsize = 8))

Remove white space in a multiple panel structure (16 graphs) to make pie charts bigger

EDIT: I am struggling with the title of this post since I am not sure anymore whether the white space is the problem. My main concern is that the pie charts are too small and I wanted to enlarge them by filling up the white margins in between them. Not sure whether this means that the pie charts are the problem, or whether the margins are!
I made the following graph:
However, the pie charts are way too small to be of added value to this graph. I gave them a grey background and in that way it is clear that there is quite some white space that remains unused. Is there a way to make the piecharts bigger in this structure?
NOTE: originally I did not want to use the white space below (left and right from the legend) but now I am okay with all possible solutions that increase the visibility of this graph!
DATA LINE CHART
outfull<-structure(list(MEt_R = c(0.0804285986755867, 0.0818290494516811,
0.08210154234207, 0.0814755456594059, 0.0831102114593095, 0.0816695357763684,
0.0813328121344033, 0.0810108624189488, 0.0818598254382056, 0.0830431177501689,
0.0816376764003817, 0.0840073817361272, 0.0853936930533085, 0.0879219679095458,
0.0854048968568587, 0.0860759049369366, 0.0882493829941351, 0.0867703899775953,
0.0865942045402731, 0.0810145344754354), MEp_R = c(0.0403177921121929,
0.0406138102668729, 0.0400078146373141, 0.037249138567354, 0.0382399652387079,
0.0361846833846079, 0.0324923094020387, 0.0321970548437844, 0.0325638786466341,
0.0316121666375798, 0.0325852079234172, 0.0332434908844657, 0.0355976600643392,
0.0393330869225411, 0.0395149563774575, 0.0411816120852081, 0.0430085728809272,
0.0403688229638535, 0.039708256572692, 0.0396710567412495), MEt_Irr = c(-0.210579335646362,
-0.20887479701005, -0.209463300907731, -0.21134881250515, -0.213848355342722,
-0.216138343894695, -0.218218940681691, -0.220626646376378, -0.220536909843105,
-0.221776071703525, -0.22166868418364, -0.22263154911707, -0.222198289987863,
-0.222405303282204, -0.221323983219654, -0.223034048807441, -0.223231412833866,
-0.220724611396726, -0.22184116121713, -0.221652312691637), MEp_Irr = c(-0.091005037463965,
-0.0871543068244998, -0.0860520133830844, -0.082885475024097,
-0.0852796850484205, -0.0791969759021717, -0.0778689132953799,
-0.0767341772452676, -0.0756081808361748, -0.0748414459803997,
-0.0754126518879104, -0.0762317968359476, -0.0755876836232721,
-0.0758199300658314, -0.0745577666874828, -0.0762573255685292,
-0.0763362684722027, -0.074483807673175, -0.0744398384221861,
-0.0744598479378566), se_MEt_Rainfed = c(0.0774330661227882,
0.0773250866974692, 0.077256061062775, 0.0771327497535611, 0.0771521985759051,
0.0708916514716712, 0.0694665306934766, 0.0694972720680839, 0.0693729541545876,
0.0699669865284523, 0.070260199431952, 0.0690461062237432, 0.0711819979382877,
0.0710875280360804, 0.0708689510634827, 0.0711765465845632, 0.0708297931421112,
0.0704738634823944, 0.0708072460217385, 0.0709953190951179),
se_MEp_Rainfed = c(0.192756229054221, 0.191856228578138,
0.192226563324443, 0.19097117645322, 0.191387874467092, 0.173921574335822,
0.165088971309669, 0.165115227802311, 0.164795115225161,
0.164140221638083, 0.164937684783056, 0.165268057600504,
0.1649368806914, 0.168713285650115, 0.168149249109913, 0.169106046673566,
0.167335760047029, 0.165795756250856, 0.165689006757723,
0.16551179995361), se_MEt_Irrigation = c(0.12059098436772,
0.120899123289279, 0.120407371708647, 0.120696007335912,
0.120177269171968, 0.123769133952132, 0.127425159597542,
0.128368483598982, 0.128864767741048, 0.128965437272282,
0.128581625585643, 0.127783623408749, 0.127472047347015,
0.12734592667827, 0.127159348618761, 0.12634060930837, 0.127889037344552,
0.128896014165611, 0.128718310299992, 0.128757570918953),
se_MEp_Irrigation = c(0.0914799237827752, 0.0949973010336107,
0.0954100933352331, 0.0961863807496929, 0.0937431456706336,
0.0984291865206365, 0.100823347187465, 0.101012516949386,
0.101921512108335, 0.102756591718654, 0.102207359450442,
0.101524016844143, 0.102057567028856, 0.10215500585099, 0.103433290392082,
0.101335657592202, 0.102157397442543, 0.103959921670023,
0.103233139264567, 0.102622388936846), Irrigationtotal = c(4925,
4918, 4914, 4906, 4902, 4893, 4890, 4882, 4877, 4871, 4866,
4860, 4857, 4854, 4844, 4841, 4834, 4830, 4825, 4817), Irrigation0 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Irrigation10 = c(529, 522, 518, 510, 506, 497, 494, 486,
481, 475, 470, 464, 461, 458, 448, 445, 438, 434, 429, 421
), Irrigation20 = c(431, 431, 431, 431, 431, 431, 431, 431,
431, 431, 431, 431, 431, 431, 431, 431, 431, 431, 431, 431
), Irrigation30 = c(349, 349, 349, 349, 349, 349, 349, 349,
349, 349, 349, 349, 349, 349, 349, 349, 349, 349, 349, 349
), Irrigation40 = c(333, 333, 333, 333, 333, 333, 333, 333,
333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333, 333
), Irrigation50 = c(373, 373, 373, 373, 373, 373, 373, 373,
373, 373, 373, 373, 373, 373, 373, 373, 373, 373, 373, 373
), Irrigation60 = c(255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
), Irrigation70 = c(288, 288, 288, 288, 288, 288, 288, 288,
288, 288, 288, 288, 288, 288, 288, 288, 288, 288, 288, 288
), Irrigation80 = c(239, 239, 239, 239, 239, 239, 239, 239,
239, 239, 239, 239, 239, 239, 239, 239, 239, 239, 239, 239
), Irrigation90 = c(274, 274, 274, 274, 274, 274, 274, 274,
274, 274, 274, 274, 274, 274, 274, 274, 274, 274, 274, 274
), Irrigation100 = c(1854, 1854, 1854, 1854, 1854, 1854,
1854, 1854, 1854, 1854, 1854, 1854, 1854, 1854, 1854, 1854,
1854, 1854, 1854, 1854)), .Names = c("MEt_R", "MEp_R", "MEt_Irr",
"MEp_Irr", "se_MEt_Rainfed", "se_MEp_Rainfed", "se_MEt_Irrigation",
"se_MEp_Irrigation", "Irrigationtotal", "Irrigation0", "Irrigation10",
"Irrigation20", "Irrigation30", "Irrigation40", "Irrigation50",
"Irrigation60", "Irrigation70", "Irrigation80", "Irrigation90",
"Irrigation100"), row.names = c(NA, 20L), class = "data.frame")
DATA PIE CHARTS
percentageIT<-structure(list(nuts0 = c("IT", "IT", "IT", "IT", "IT", "IT",
"IT", "IT", "IT", "IT"), variable = structure(1:10, .Label = c("percentage_irri10",
"percentage_irri20", "percentage_irri30", "percentage_irri40",
"percentage_irri50", "percentage_irri60", "percentage_irri70",
"percentage_irri80", "percentage_irri90", "percentage_irri100"
), class = "factor"), value = c(0.108497262218617, 0.0874062056378017,
0.0707767187183127, 0.0675319407828027, 0.0756438856215778, 0.0517136483471912,
0.0584060028391807, 0.0484688704116812, 0.0555668221456094, 0.375988643277226
)), .Names = c("nuts0", "variable", "value"), row.names = c(10L,
25L, 40L, 55L, 70L, 85L, 100L, 115L, 130L, 145L), class = "data.frame")
PIE CHART
library(ggplot2)
percentlabelsIT<- round(100*percentageIT$value, 0)
pielabelsIT<- paste(percentlabelsIT, "%", sep="")
l12<-ggplot(data=percentageIT, aes(x=factor(1), y=percentlabelsIT, fill=factor(variable))) +
geom_bar(width=1, stat="identity") +
coord_polar(theta="y") +
ylab("") +
xlab("") +
labs(fill="") +
theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
legend.position="none",
line = element_blank()
)+
geom_text(aes(x = 2.3, # Solution for part 2,
y = percentlabelsIT / 2 + c(0, cumsum(percentlabelsIT)[-length(percentlabelsIT)]),
label=pielabelsIT))+
scale_fill_manual(values=cols)+ggtitle("IT")
cols <- c("yellow","greenyellow","#00FF00", "#00C639","#00AA55", "#00718E", "#0055AA", "#001CE3","blue4","midnightblue")
MAKE LINE CHART
library(ggplot2)
library(gtable)
library(reshape2)
out121<-outfull
# line plot
out121$perc1<-c(1:20)
l64<-ggplot(out121,aes(perc1))
l65<-l64+geom_line(aes(y=MEt_R,colour="Rainfed"),size=1.3)+
geom_line(aes(y=MEt_R+se_MEt_Rainfed,colour="Rainfed range"),size=0.7)+
geom_line(aes(y=MEt_R-se_MEt_Rainfed,colour="Rainfed range"),size=0.7)+
geom_line(aes(y=MEt_Irr,colour="Irrigation"),size=1.3)+
geom_line(aes(y=MEt_Irr+se_MEt_Irrigation,colour="Irrigation range"),size=0.7)+
geom_line(aes(y=MEt_Irr-se_MEt_Irrigation,colour="Irrigation range"),size=0.7)+
scale_colour_manual(values=c("blue3","mediumslateblue","green3","green"), name="MEt")+
scale_x_discrete(name="Threshold irrigation (in percentage)",breaks=c(0, 250, 500,750,1000),
labels=c("0", "25", "50","75","100")) +
scale_y_continuous(name="MEt",limits = c(-0.7, 0.5),breaks=c(-0.3,-0.1,0,0.1,0.3,0.5))
l66<-l65+ theme_bw()+ggtitle("Full sample") +
theme(plot.title = element_text(lineheight=.8, face="bold"),legend.position="bottom")+
guides(col=guide_legend(ncol=2,title.position="top"))+
theme(panel.background = element_rect(fill = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
l66
out121$perc<-c(1:20)
out121$perc<- as.character(out121$perc)
out121$perc <- factor(out121$perc, levels=unique(out121$perc))
test <- data.frame(out121$perc,out121$Irrigation10,out121$Irrigation20,out121$Irrigation30,out121$Irrigation40,
out121$Irrigation50,out121$Irrigation60,out121$Irrigation70,out121$Irrigation80,
out121$Irrigation90,out121$Irrigation100)
# barplot(as.matrix(test))
library(reshape2)
foo.long<-melt(test)
foo.long$out121.perc<- as.character(foo.long$out121.perc)
foo.long$out121.perc <- factor(foo.long$out121.perc, levels=unique(foo.long$out121.perc))
cbbPalette <- c("yellow","greenyellow","#00FF00", "#00C639","#00AA55", "#00718E", "#0055AA", "#001CE3","blue4","midnightblue")
l2 <- ggplot(foo.long, aes(out121.perc,value,fill=variable))+
geom_bar(position="stack",stat="identity")+
scale_fill_manual(values=cbbPalette,name = "% of irrigation",
labels = c("0-10% irrigation", "10-20% irrigation", "20-30% irrigation",
"30-40% irrigation", "40-50% irrigation", "50-60% irrigation",
"60-70% irrigation", "70-80% irrigation", "80-90% irrigation",
"90-100% irrigation"))+
scale_x_discrete(name="Threshold irrigation (in percentage)",breaks=c(0, 250, 500,750,1000),
labels=c("0", "25", "50","75","100")) +
scale_y_continuous(name="number of farms",limits = c(0, 30000), breaks=c(0, 2000,4000,6000,8000,10000,12000))+
theme(legend.position="bottom", panel.background = element_rect(fill = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())+
guides(fill=guide_legend(title="Number of irrigated farms",ncol=5, title.position = "top"))
l2
# ggplotGrob
g1 <- ggplotGrob(l2)
g2 <- ggplotGrob(l66)
# Add plots together
pp <- c(subset(g2$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g2, g1$grobs[[which(g1$layout$name == "panel")]], pp$t,
pp$l, pp$b, pp$l)
# Add second axis for accuracy
ia <- which(g1$layout$name == "axis-l")
ga <- g1$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g1$widths[g1$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
# Add second y-axis title
ia <- which(g1$layout$name == "ylab")
ax <- g1$grobs[[ia]]
# str(ax) # you can change features (size, colour etc for these -
# change rotation below
ax$rot <- 270
g <- gtable_add_cols(g, g1$widths[g1$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
# extract legend
leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]]
leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]]
legc = gtable:::cbind_gtable(leg1, leg2, "first")
g$grobs[[which(g$layout$name == "guide-box")]] <-
gtable:::cbind_gtable(leg1, leg2, "first")
grid.draw(g) # Note: Legend does not fit
g$heights[[6]] = unit(3, "cm") # Add more space for the legend - can adjust this to suit
grid.draw(g)
COMBINE PIECHART AND LINE CHART (so probably the solution to the question is in this part of the code)
library(gridExtra)
library(grid)
l <- cbind(c(5:1, NA), rbind(6:10, matrix(16, 5, 5)), c(11:15, NA))
g12 <- ggplotGrob(l12)
grid.arrange(grobs=list(g12,g12,g12,g12,g12,g12,g12,g12,g12,g12,g12,g12,g12,g12,g12,g),
layout_matrix=l)

Ggplot: Same data not plotting aline plot, but making a bar plot (Each group consist of only one observation.)

I'm trying to create a line plot from the following data:
> dput(agdata)
structure(list(date = c("2014-11-30", "2014-12-01", "2014-12-02",
"2014-12-03", "2014-12-04", "2014-12-05", "2014-12-06", "2014-12-07",
"2014-12-14", "2014-12-15", "2014-12-16", "2014-12-17", "2014-12-18"
), A = c(86.3333333333333, 91.1666666666667, 83.4, 83, 86, 94.75,
78, 87, 87, 92, 98.6, 87, 85.3333333333333), B = c(1015.16666666667,
1014.33333333333, 1017.2, 1021, 1017.5, 1021.5, 1029, 1022, 1009,
1012.4, 1014.8, 1011, 1011), C = c(8.55666666666667, 7.145, 7.51,
4.61, 4.335, 3.2625, 6.585, 8.35, 9.09, 6.48, 2.532, 11.74, 11.7933333333333
), D = c(24, 74.6666666666667, 77, 57.5, 82.5, 56.25, 0, 88,
32, 61, 50, 92, 80.6666666666667)), .Names = c("date", "A", "B",
"C", "D"), row.names = c(NA, -13L), class = "data.frame")
I tried this:
ggplot(data = agdata,aes(x = date, y = A)) + geom_line(stat="identity")
and various other parameters, including removing the stat parameter, moving aes to geom_line, and a few others.
I keep getting:
geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
To check if the data is fine, I tried:
ggplot(data = agdata,aes(x = date, y = A)) + geom_bar(stat="identity")
which works just fine.
Any pointers as to what I'm missing here? I have a feeling it has something to do with a group= parameter in aes() by looking at this, and this, but not sure what.
Ugh, nevermind. Figured it the minute I posted this. it gas to be group=1 in aes().
Feel rockheaded now.

Adding title and sub title to Venn Diagram

I am trying to add a main and sub title to my venn diagram that I created using the following code in R. I have read through the R ‘VennDiagram’ package documentation without success. I've also tried using gird.arrange() with a textGrob and the graph from the resulting code, but received an error stating that all inputs must be grobs.
require(VennDiagram)
draw.triple.venn(
area1 = 396,
area2 = 273,
area3 = 147,
n12 = 266,
n23 = 86,
n13 = 143,
n123 = 83,
category = c("Study", "Work", "Play"),
fill = c("blue", "red", "green"),
euler.d=TRUE,
scaled=TRUE
)
The output of that function is a gList (and a side-effect of plotting, if you don't specify ind=FALSE). In order to use it with grid.arrange, you'd need to wrap it in a gTree,
g = draw.triple.venn(
area1 = 396,
area2 = 273,
area3 = 147,
n12 = 266,
n23 = 86,
n13 = 143,
n123 = 83,
category = c("Study", "Work", "Play"),
fill = c("blue", "red", "green"),
euler.d=TRUE,
scaled=TRUE, ind = FALSE,
)
require(gridExtra)
grid.arrange(gTree(children=g), top="Title", bottom="subtitle")

Resources