I have the following ggplot and trying to add the legend and a geom_abline at median.
purple line is for 2013 prods and red is for 2014
This is what I did to generate the plot:
ggplot(prods[Year==2013,], aes(x = Date, y = Prod, group = SOM)) +
geom_line(lwd = 1.3, colour = "purple") +
ylab("Actual Productivity") + theme(axis.title.x=element_blank()) +
geom_line(data=prods[Year==2014,], aes(x = Date, y = Prod, group = SOM),lwd = 1.3, colour = "red") +
geom_abline(data = prods,h=median(Prod))+
scale_color_manual("Period", values = c("purple","red"), labels = c("2013","2014")) +
facet_wrap(~ SOM)
I am not getting any error but there is no legend nor abline is popping up on the image. Plot looks like
this:
any help would be highly appreciated.
regards,
As per aosmith's advice:
I did the following and was able to get the following plot:
ggplot(data=prods,aes(Date)) +
geom_line(data=prods[Year==2013,],aes(y=Prod, colour="red"),lwd = 1.3,) +
geom_line(data=prods[Year==2014,],aes(y=Prod, colour="blue"),lwd = 1.3) +
geom_hline(data=prods, aes(yintercept = median(Prod))) +
scale_colour_manual(name="Period",values=c("blue","red", "black"), labels = c("2014","2013", "Median")) +
ylab("Actual Prod") + xlab(" ") +
theme(axis.title.y = element_text(size = 15, vjust=0.3)) +
facet_wrap(~ SOM)
Plot looks like this:
Related
As described, I want to add a legend to my graph.
nice_plot <- ggplot() +
geom_line(data = plot_dataframe_SD1, mapping = aes(x = XValues, y = YValues_SD1), color = "blue") +
geom_line(data = plot_dataframe_SD2, mapping = aes(x = XValues, y = YValues_SD2), color = "green") +
xlim(1, 2) +
ylim(1, 5) +
xlab("Standarard Deviation") +
ylab("AV") +
scale_fill_identity(name = 'the fill', guide = 'legend',labels = c('m1')) +
scale_colour_manual(name = 'the colour',
values =c('blue'='blue','green'='green'), labels = c('c2','c1'))
This is my code so far, btut it won't output a legend.
I also don't get an error message.
What is wrong with the code or what do I miss out?
I am trying to plot a line plot in r by using ggplot. Unfortunately, the legend does not show up. Can anyone help me?
My code looks like the following:
dfdatavgsM=data.frame(datum, avgsätzegespMT, avgsätzegespML)
ggplot(data = dfdatavgsM, aes(x=datum, color=Wettbewerbsart))
+ geom_line(data=dfdatavgsM, aes(y = avgsätzegespML),color="red")
+ geom_line(data=dfdatavgsM, aes(y = avgsätzegespMT), color="blue")
+ geom_vline(xintercept=2011, size = 0.6)
+ scale_y_continuous(name="Anzahl an Sätzen")
+ scale_x_datetime(name = "Saison" ,date_breaks = ("2 year"),date_labels = "%Y")
+ ggtitle("Wettbewerbsintensität in Spielen mit |∆TTR| ≤ 118") + theme(panel.background = element_rect(fill = "white", colour = "black"))
+ theme(panel.grid.major = element_line(size = 0.25, linetype = 'solid', colour = "light grey")) + theme(axis.ticks = element_line(size = 1))
The legend will only appear if you use color inside an aes statement. You will need to reshape your data to 'long' format (e.g. with tidyr::gather), and have a single geom_line term and an aes including color=type. Something like this (not tested as I don't have your data)...
library(tidyverse)
dfdatavgsM=data.frame(datum, avgsätzegespMT, avgsätzegespML)
dfdatavgsMlong <- dfdatavgsM %>% gather(key = type, value = value, -datum)
ggplot(data = dfdatavgsMlong, aes(x=datum, y=value, color=type)) +
geom_line()
I am trying to make an overlapping histogram like this:
ggplot(histogram, aes = (x), mapping = aes(x = value)) +
geom_histogram(data = melt(tpm_18_L_SD), breaks = seq(1,10,by = 1),
aes(y = 100*(..count../sum(..count..))), alpha=0.2) +
geom_histogram(data = melt(tpm_18_S_SD), breaks = seq(1,10,by = 1),
aes(y = 100*(..count../sum(..count..))), alpha=0.2) +
geom_histogram(data = melt(tpm_18_N_SD), breaks = seq(1,10,by = 1),
aes(y = 100*(..count../sum(..count..))), alpha=0.2) +
facet_wrap(~variable, scales = 'free_x') +
ylim(0, 20) +
ylab("Percentage of Genes") +
xlab("Standard Deviation")
My code can only make them plot side by side and I would like to also make them overlap. Thank you! I based mine off of the original post where this came from but it did not work for me. It was originally 3 separate graphs which I combined with grid and ggarrange. It looks like this right now.
Here is the code of the three separate graphs.
SD_18_L <- ggplot(data = melt(tpm_18_L_SD), mapping = aes(x = value)) +
geom_histogram(aes(y = 100*(..count../sum(..count..))), breaks = seq(1, 10, by = 1)) +
facet_wrap(~variable, scales = 'free_x') +
ylim(0, 20) +
ylab("Percentage of Genes") +
xlab("Standard Deviation")
SD_18_S <- ggplot(data = melt(tpm_18_S_SD), mapping = aes(x = value)) +
geom_histogram(aes(y = 100*(..count../sum(..count..))), breaks = seq(1, 10, by = 1)) +
facet_wrap(~variable, scales = 'free_x') +
ylim(0, 20) +
ylab("Percentage of Genes") +
xlab("Standard Deviation")
SD_18_N <- ggplot(data = melt(tpm_18_N_SD), mapping = aes(x = value)) +
geom_histogram(aes(y = 100*(..count../sum(..count..))), breaks = seq(1, 10, by = 1)) +
facet_wrap(~variable, scales = 'free_x') +
ylim(0, 20) +
ylab("Percentage of Genes") +
xlab("Standard Deviation")
What my graphs look like now:
ggplot expects dataframes in a long format. I'm not sure what your data looks like, but you shouldn't have to call geom_histogram for each category. Instead, get all your data into a single dataframe (you can use rbind for this) in long format (what you're doing already with melt) first, then feed it into ggplot and map fill to whatever your categorical variable is.
Your call to facet_wrap is what puts them in 3 different plots. If you want them all on the same plot, take that line out.
An example using the iris data:
ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_histogram(alpha = 0.6, position = "identity")
I decreased alpha in geom_histogram so you can see where colors overlap, and added position = "identity" so observations aren't being stacked. Hope that helps!
I have a data frame d like this:
d <- data.frame("name" = c("pippo","pluto","paperino"),
"id" = c(1,2,3),"count" = c(10,20,30),
"pvalue"=c(0.01,0.02,0.05),
geneRatio=c(0.5,0.8,0.2),
type=c("KEGG","Reactome","Reactome"))
and I plot a dotplot using the library ggplot:
ggplot(data = d,aes(geneRatio,name,size=count,colour = pvalue)) +
geom_point()+
ggtitle("Significantly Pathways") +
xlab("Gene Ratio") +
ylab("Pathways")+
theme(axis.text.y = element_text(color=d$type))
This is the plot at the moment
I would like to add to legend the information of "type" contained in dataframe d.
I would like to have a new item in the legend with color red = Reactome and color black= KEGG
Not saying that this is a good idea, but you can add a nonsensical geom to force the adding of a guide:
d <- data.frame("name" = c("pippo","pluto","paperino"),
"id" = c(1,2,3),
"count" = c(10,20,30),
"value"=c(0.01,0.02,0.05),
geneRatio=c(0.5,0.8,0.2),
type=c("KEGG","Reactome","Reactome")
)
library(ggplot2)
ggplot(data = d, aes(geneRatio,name,colour = pvalue)) +
geom_point(aes(size=count))+
geom_polygon(aes(geneRatio,name,fill = type)) +
ggtitle("Significantly Pathways") +
xlab("Gene Ratio") +
ylab("Pathways") +
scale_fill_manual(values = c('Reactome'='red', 'KEGG'='black')) +
theme(axis.text.y = element_text(color=d$type))
geom_polygon may not work with your actual data, and you may not find a suitable 'nonsensical' geom. I agree with #zx8754, a facet would be clearer:
ggplot(data = d, aes(geneRatio,name,colour = pvalue)) +
geom_point(aes(size=count)) +
ggtitle("Significantly Pathways") +
xlab("Gene Ratio") +
ylab("Pathways") +
facet_grid(type ~ ., scales = 'free_y', switch = 'y')
You could accomplish this using annotate, but it is a bit manual.
ggplot(data = d, aes(geneRatio, name, size = count, colour = pvalue)) +
geom_point() +
ggtitle("Significantly Pathways") +
xlab("Gene Ratio") +
ylab("Pathways")+
theme(axis.text.y = element_text(color=d$type)) +
annotate("text", x = 0.25, y = 3.5, label = "Reactome", color = "red") +
annotate("text", x = 0.25, y = 3.4, label = "KEGG", color = "black")
I'm currently using the ggplot package to plot a histogram of normal variates with a N(0, 1) density overlay. I'm very new to this package and the code I'm using is
x = rnorm(1000)
qplot(x, geom = 'blank') +
geom_histogram(aes(y = ..density.., colour = 'Histogram'), legend = FALSE,
binwidth = 0.5, fill = "blue") +
stat_function(fun = dnorm, aes(colour = 'Density'))+
scale_x_continuous('x', limits = c(-4, 4))+
opts(title = "Histogram with Overlay")+
scale_colour_manual(name = 'Legend', values = c('darkblue', 'red')) +
scale_y_continuous('Frequency')+
opts(legend.key=theme_rect(fill="white",colour="white"))+
opts(legend.background = theme_rect())
This code produces the following diagram. How do I change the legend so that the line representing the histogram is replaced with a filled blue box (that represents the bars of the histogram)? Thank You!
Maybe something like this...
dat = data.frame(x=rnorm(1000))
ggplot(dat,aes(x=x)) +
geom_histogram(aes(y=..density..,fill="Histogram"),binwidth=0.5) +
stat_function(fun = dnorm, aes(colour= "Density")) +
scale_x_continuous('x', limits = c(-4, 4)) +
opts(title = "Histogram with Overlay") +
scale_fill_manual(name="",value="blue") +
scale_colour_manual(name="",value="red") +
scale_y_continuous('Frequency')+
opts(legend.key=theme_rect(fill="white",colour="white"))+
opts(legend.background = theme_blank())
Note: Since version 0.9.2 opts has been replaced by theme. So for example, the last two lines above would be:
theme(legend.key = element_rect(fill = "white",colour = "white")) +
theme(legend.background = element_blank())