R stacked bar graph plotting geom_text - r

I'm trying to plot a stacked bar graph in R using ggplot. I also want to include percentage in each piece of bars for that piece. I tried to follow the posts 1, 2, 3 but the values are not exactly in their respective blocks. My data is a file in dropbox.
My code is as follows:
f<-read.table("Input.txt", sep="\t", header=TRUE)
ggplot(data=f, aes(x=Form, y=Percentage, fill=Position)) +
geom_bar(stat="identity", colour="black") +
geom_text(position="stack", aes(x=Form, y=Percentage, ymax=Percentage, label=Percentage, hjust=0.5)) +
facet_grid(Sample_name ~ Sample_type, scales="free", space="free") +
opts(title = "Input_profile",
axis.text.x = theme_text(angle = 90, hjust = 1, size = 8, colour = "grey50"),
plot.title = theme_text(face="bold", size=11),
axis.title.x = theme_text(face="bold", size=9),
axis.title.y = theme_text(face="bold", size=9, angle=90),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank()) +
scale_fill_hue(c=45, l=80)
ggsave("Output.pdf")
The output is-
Any help is greatly appreciated.
Thank you for your help and time!

I think you're using an older version of ggplot2. Because with your code modified for ggplot2 v 0.9.3, I get this:
p <- ggplot(data = df, aes(x = Form, y = Percentage, fill = Position))
p <- p + geom_bar(stat = "identity", colour = "black")
p <- p + geom_text(position = "stack", aes(x = Form, y = Percentage, ymax = Percentage, label = Percentage, hjust = 0.5))
p <- p + facet_grid(Sample_name ~ Sample_type, scales="free", space="free")
p <- p + theme(plot.title = element_text("Input_profile"),
axis.text.x = element_text(angle = 90, hjust = 1, size = 8, colour = "grey50"),
plot.title = element_text(face="bold", size=11),
axis.title.x = element_text(face="bold", size=9),
axis.title.y = element_text(face="bold", size=9, angle=90),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
p <- p + scale_fill_hue(c=45, l=80)
p
You see that the text objects are normally placed properly. There are places where the bars are just too short so that the numbers overlap. You can also play with the size parameter.
To rectify that, you could do something like this to add up the numbers by yourself.
df <- ddply(df, .(Form, Sample_type, Sample_name), transform,
cum.perc = Reduce('+', list(Percentage/2,cumsum(c(0,head(Percentage,-1))))))
p <- ggplot(data = df, aes(x = Form, y = Percentage, fill = Position))
p <- p + geom_bar(stat = "identity", colour = "black")
p <- p + geom_text(aes(x = Form, y = cum.perc, ymax = cum.perc, label = Percentage, hjust = 0.5), size=2.7)
p <- p + facet_grid(Sample_name ~ Sample_type, scales="free", space="free")
p <- p + theme(plot.title = element_text("Input_profile"),
axis.text.x = element_text(angle = 90, hjust = 1, size = 8, colour = "grey50"),
plot.title = element_text(face="bold", size=11),
axis.title.x = element_text(face="bold", size=9),
axis.title.y = element_text(face="bold", size=9, angle=90),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
p <- p + scale_fill_hue(c=45, l=80)
p
This gives:

Here a solution using barchart from lattice.
library(latticeExtra)
barchart(Percentage~Form|Sample_type*Sample_name,data=dat,
groups =Position,stack=T,
panel=function(...){
panel.barchart(...)
ll <- list(...)
keep <- !is.na(ll$groups[ll$subscripts])
x <- as.numeric(ll$x[keep])
y <- as.numeric(ll$y[keep])
groups <- as.numeric(factor(ll$groups)[ll$subscripts[keep]])
for (i in unique(x)) {
ok <- x == i
ord <- sort.list(groups[ok])
pos <- y[ok][ord] > 0
nok <- sum(pos, na.rm = TRUE)
h <- y[ok][ord][pos]
panel.text(x = rep(i, nok),y = cumsum(h)-0.5*h,
label = h,cex=1.5)
}
},
auto.key = list(columns = 5),
par.settings = ggplot2like(n = 5),
lattice.options = ggplot2like.opts())

Related

Having trouble aligning histogram and density plots in ggplot2 with cowplot

Only the stripped down density plot appears in the output:
library(ggplot2)
library(cowplot)
raises <- attitude$raises
df <- data.frame(raises)
ph <- ggplot(df, aes(x = raises)) +
geom_histogram(binwidth = 1, color = "black", fill = "light grey") +
scale_x_continuous(breaks = seq(40, 90, by = 10)) +
ylim(0,3) +
theme_classic()
pd <- ggplot(df, aes(x = raises)) +
geom_density() +
scale_x_continuous(breaks = seq(40, 90, by = 10)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1)), position = "right") +
theme_classic() +
theme(axis.line.x = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())
alpl <- align_plots(ph, pd, align = "hv", axis = "tblr")
ggdraw(alpl[[1]]) + draw_plot(alpl[[2]])
I would like to overlay them so that you can see the histogram with the frequency as well as the density.
You have an opaque plot background and panel background in your density plot which you need to remove in theme. Obviously, I don't have your data, but the following code gives you the idea:
library(ggplot2)
library(cowplot)
set.seed(1)
df <- data.frame(raises = c(rnorm(100, 65, 10)))
ph <- ggplot(df, aes(x = raises)) +
geom_histogram(binwidth = 1, color = "black", fill = "light grey") +
scale_x_continuous(breaks = seq(40, 90, by = 10), limits = c(40, 90)) +
ylim(0,3) +
theme_classic()
pd <- ggplot(df, aes(x = raises)) +
geom_density() +
scale_x_continuous(breaks = seq(40, 90, by = 10), limits = c(40, 90)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1)), position = "right") +
theme_classic() +
theme(axis.line.x = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
plot.background = element_blank(),
panel.background = element_blank())
alpl <- align_plots(ph, pd, align = "hv", axis = "tblr")
ggdraw(alpl[[1]]) + draw_plot(alpl[[2]])

Label individual plots in facet_grid

I am trying to finish this graph, and what I need is to add the R-squared to each graph individually.
However, the labels are superimposed on each graph.
I used p + geom_text and p + geom_label without positive results.
Trajectories$date<-as.POSIXct(Trajectories$date,"%Y-%m-%d",tz = "UTC")
# Reorder names in a new variable
Trajectories$variable_f = factor(Trajectories$variable,
levels=c("canopy_cover", "Leaf_litter", "Chla", "Shrimps", "macroinvertebrates"))
levels(Trajectories$variable_f) <-
c("textstyle('Canopy openness')",
"textstyle('Leaf litter')",
"textstyle('Chlorophyll-')*italic('a')",
"textstyle('Shrimps')",
"atop(NA,atop(textstyle('Macroinvertebrate'),textstyle('density')))")
# Changes names in Facet_grid ---- es una manera buena, pero no la voy a usar --- Habria que labeller(variable_f = variable_f
#variable_f <- c("Canopy openness", "Leaf litter", "Chlorophyll-a","Shrimps","Macroinvertebrates")
#names(variable_f) <- c("canopy_cover", "Leaf_litter", "Chla", "Shrimps", "macroinvertebrates")
streams <- c("Prieta A", "Prieta B")
names(streams) <- c("QPA", "QPB")
# General graph -----------------------------------------------------------
p<- ggplot(Trajectories, aes(date,value)) +
geom_point(shape = 21, fill = "#bdd7e7", color = "#2171b5", size = 3) +
geom_smooth(se = T, size=1.7, color= "gray20", method = "gam", formula = y ~s(x)) +
geom_hline(yintercept = 0, color="gray20") +
xlab('Year') + ylab("Change in magnitude") +
theme(axis.title.x = element_text(size = 14, angle = 0)) + # axis x
theme(axis.title.y = element_text(size = 14, angle = 90)) + # axis 7
theme(axis.text.x=element_text(angle=0, size=10, vjust=0.5, color="black")) + #subaxis x
theme(axis.text.y=element_text(angle=0, size=10, vjust=0.5, color="black")) + #subaxis y
ylim(-3,3) +
theme(legend.position="none")+
theme(panel.grid.major = element_line(colour = "gray95"), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) +
theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5)) +
facet_grid(vars(stream), vars(variable_f),
labeller = labeller(variable_f = label_parsed, stream = streams)) +
theme(
strip.text.x = element_text(size = 10, color = "black"),
strip.text.y = element_text(size = 10, color = "black"),
strip.placement = "outside") +
theme(strip.background=element_rect(color= "black", fill="gray85")) +
theme(strip.text.x = element_text(margin = margin(0.001,0,0.001,0, "cm"))) +
geom_vline(aes(xintercept=as.POSIXct("2017-09-21")), # Hurricane Maria
col= "red",linetype=4, alpha=0.9) +
geom_vline(aes(xintercept=as.POSIXct("2017-09-6")), # Hurricane Irma
col= "blue",linetype=4, alpha=0.9)
p
I tried to build a data frame with the labels. But it doesn't work, and it overlaps the labels.
labels <- data.frame(variable =c("canopy_cover","Leaf_litter","Chla","Shrimps","macroinvertebrates",
"canopy_cover","Leaf_litter","Chla","Shrimps","macroinvertebrates"),
label=c("R1","R2","R3","R4","R5","R6","R7","R8","R9","R10"),
stream= c("QPA","QPA","QPA","QPA","QPA",
"QPB","QPB","QPB","QPB","QPB"))
labels
p + geom_text(
size = 5,
data = labels,
mapping = aes(x = as.POSIXct("2017-09-6"), y = Inf, label = label),
hjust = 1.05,
vjust = 1.5
)
Thanks in advance

Connected points in ggplot boxplot

I'm trying to create a simple boxplot with connected lines similar to the one described in this question: Connect ggplot boxplots using lines and multiple factor.
However, the interaction term in that example produces an error:
geom_path: Each group consists of only one observation. Do you need to
adjust the group aesthetic?
I would like to connect each point using the index variable. Here is the code:
group <- c("A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B")
session <- c("one","two","one","two","one","two","one","two","one","two","one","two","one","two","one","two","one","two","one","two")
value <- c(1.02375,1.01425,1.00505,0.98105,1.09345,1.09495,0.98255,0.90240,0.99185,0.99855,0.88135,0.72685,0.94275,0.84775,1.01010,0.96825,0.85215,0.84175,0.89145,0.86985)
index <- c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10)
df <- data.frame(group,session,value,index)
# Graph plots
p <- ggplot(df, aes(x=group, y=value, fill=session))
p <- p + geom_boxplot(color="grey40", outlier.alpha=0.0) #alpha=0.6
p <- p + stat_summary(fun.y=mean,geom="point",pch="-",color="white",size=8, position = position_dodge(width=0.75)) # size=2 color="black"
p <- p + geom_point(size=2, alpha=0.6, aes(group=session), data=df, position = position_dodge(width=0.75))
p <- p + geom_line(aes(group = index), alpha = 0.6, colour = "black", position = position_dodge(width=0.75), data=df) #
p <- p + scale_fill_manual(values=c("#969696","#74c476"))
p <- p + theme(
axis.text.x = element_text(colour = "black"), #angle = 60, hjust = 1
axis.text.y = element_text(colour = "black"),
axis.title.x = element_blank(), #element_text(colour = "black"),
axis.title.y = element_text(colour = "black"),
legend.position = "none"
#panel.background = element_blank(), #element_rect(fill="white", colour="black", size=2),
#panel.grid.major = element_blank(),
#panel.grid.minor = element_blank(),
#panel.border = element_blank(),
#axis.line = element_line(size=1.5, colour = "black")
#panel.grid.major = element_line(size = .5, colour = "grey")
)
ggsave("~/Desktop/test.pdf", width=4, height=6, units=c("in"), plot=p)
However, that produces only vertical lines as in this image:
Some changes analogous as in my other answer:
df <- data.frame(group, session, value, index, U = interaction(session, group))
p <- ggplot(df, aes(x = U, y = value, fill = session)) +
scale_x_discrete(labels = rep(unique(group), each = 2))
p <- p + geom_line(aes(group = index), alpha = 0.6, colour = "black", data = df)
# no need for dodge
The rest is the same as in your code.
(The remaining vertical lines are from the boxplot.)

y axis on the rigth side of a bar plot

Running in R this code:
# example data
x = runif(14, 0.0, 1.0)
norm = x/sum(x)
data = data.frame(replicate(17,sample(norm)))
require(ggplot2)
require(reshape2)
data$no <- seq_len(nrow(data))
data_molten <- melt(data, id.vars = "no")
data_molten[data_molten == 0] <- NA
View(data_molten)
# ggplot
ggplot(data_molten[!is.na(data_molten$value),], aes(x = variable, y = value, fill = factor(no))) +
geom_bar(stat = "identity") +
scale_fill_hue(l=40) + facet_grid(no~.) + theme_minimal() +
theme(legend.position="none",
axis.text.x = element_text(angle = 90, colour="black", vjust = 0.5, hjust=1, size=16),
axis.title.x = element_blank(), axis.title.y = element_blank(),
axis.line.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(),
strip.text.y=element_text(size = 16, colour="black", family="", angle=00,hjust = 0),
panel.grid=element_blank(),
axis.line=element_line(size = 1, colour = "black", linetype = "solid"),
axis.ticks.x=element_line(size = 1, colour = "black", linetype = "solid"),
panel.background=element_blank())
produces this plot:
Anybody knows how can I add a vertical line between the right side of the plot and the layers' labels (the 1, 2, 3, ..., 14) which have a tick per layer's label, keeping enough space between the plot, the axis and the labels?
EDIT:
Running
num_var_x = 14
num_var_y = 17
x = runif(num_var_x, 0.0, 1.0)
norm = x/sum(x)
data = data.frame(replicate(num_var_y,sample(norm)))
## preparing dataset for ggplot
require(ggplot2)
require(reshape2)
data$no <- seq_len(nrow(data))
data_molten <- melt(data, id.vars = "no")
data_molten_sort = data_molten[with(data_molten,order(no)),]
## removing elements from variable 'no' whose max. value is e.g. < 0.025
sequence = seq(from=1, to=(num_var_y*num_var_x-num_var_x)+1, by=num_var_x)
for(i in 1:length(sequence))
{
if(isTRUE((max(data_molten_sort$value[(sequence[i]):((num_var_x+sequence[i])-(1))])) < 0.025))
{
data_molten_sort$value[(sequence[i]):((num_var_x+sequence[i])-(1))] = NA
}
}
View(data_molten)
## preparing posterior exporting
#install.packages("Cairo"); "cairo" type in png() has a better quality
library("Cairo")
#preparing exporting
png(file="ggplot.png",type="cairo", width = 4, height = 5, units = 'in',pointsize=8,res=600)
## plotting
ggplot(data_molten[!is.na(data_molten$value),], aes(x = variable, y = value, fill = factor(no))) +
geom_bar(stat = "identity") +
scale_fill_hue(l=40) + facet_grid(no~., as.table=FALSE, scale="free_y", space = "free_y") + theme_minimal() +
geom_vline(xintercept=max(as.numeric(data_molten$variable)) + 0.586, size=0.3) +
theme(legend.position="none",
axis.text.x = element_text(angle = 90, colour="black", vjust = 0.4, hjust=1, size=8),
axis.title.x = element_blank(), axis.title.y = element_blank(),
axis.line.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(),
strip.text.y=element_text(size = 8, colour="black", family="", angle=00,hjust = 0.1),
panel.grid=element_blank(),
axis.line=element_line(size = 0.3, colour = "black", linetype = "solid"),
axis.ticks.x=element_line(size = 0.3, colour = "black", linetype = "solid"),
panel.background=element_blank(), panel.margin = unit(0, "lines"))
## exporting barplot "ggplot.png" to directory
dev.off()
now the y line is continuous, plus other additions such as an exporting command.
http://i.imgur.com/C6h5fPg.png?1
Assuming that your original plot is saved in an object p1, this could be a solution:
p1 + geom_vline(xintercept=max(as.numeric(data_molten$variable))+0.5, size=1) + #data-dependent
scale_x_discrete(expand=c(0,0))+ #create nicer corner
theme(
#move labels slightly to the right with hjust
strip.text.y=element_text(size = 16, colour="black", family="", angle=00,hjust = 0.5)
)

import image,lay filter on top and then plot data

I would like to plot data on top of an imported picture in R. I would also like to lay a filtering layer on the image if possible, like controlling transparency color in ggplot2 with the alpha value:
here is an example to work on:
library(ggplot2)
library(scales)
library(jpeg)
library(scales)
library(grid)
#picture from internet
myurl <- "http://upload.wikimedia.org/wikipedia/commons/9/95/Apollonian_spheres.jpg"
z <- tempfile()
download.file(myurl,z,mode="wb")
pic <- readJPEG(z)
file.remove(z) # cleanup
x <- sample(1:10, replace=T, 10)
y <- c("a","b","c","d","e","f","g","h","i", "j")
df <- data.frame(y,x)
p <-ggplot(df, aes(y, x, fill=y)) +
annotation_custom(rasterGrob(pic, width=unit(1,"npc"), height=unit(1,"npc")),
-Inf, Inf, -Inf, Inf) +
geom_bar(stat = "identity", fill="red",width=0.8, alpha=0.75 )+
#geom_text(aes(label=data2$Attributes), vjust=1.5,colour="black")
ggtitle("Something")+ theme_classic() +
labs(y = "yyy", x = "xxx") + guides(fill = guide_legend(reverse=TRUE))+
theme(axis.text.y = element_blank()) + theme(plot.title = element_text(size=20))+
theme(axis.title.x = element_text(size = 16))+ theme(axis.title.y = element_text(size = 16))+
theme(legend.text = element_text( size = 14)) + theme(legend.title = element_text( size = 16))+
theme(panel.grid.major = element_line(colour = "white", linetype = "dotted"))
p
This results in a picture:
any ideas?

Resources