I have a problem with adding a legend to my plot. My code below:
ggplot(data = dane2, aes(x = iterations, y = powers, color = 'Moc testu')) +
geom_line(col = 'red')+
geom_line(data = dane3, aes(x = iterations, y = blad_II_r, color = 'Błąd II rodzaju')
, color = 'orange')+
ggtitle('Błąd II rodzaju i moc testu w zależności od ilości iteracji') +
xlab('iteracja') +
ylab('') +
ylim(-0.01, 1) +
scale_color_manual(name = "", values = c("Moc testu" = "red", "Błąd II rodzaju" = "orange"))+
guides(color = guide_legend(title = "Legenda")) +
theme(legend.position = 'right')
The legend doesn't show and I don't know why. Could you help?
I tried to plot with this code and it still doesn't work.
Thanks in advance!
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 new to R but got attracted by the immediate nice visualization tools.
I am looking for a nice way to fade scatter points by decreasing in size over time. This is my code:
# Reading libraries
library(gganimate)
library(ggplot2)
library("readxl")
# Reading dataframe
df1 <- read_excel("prices.xlsx")
# Create a group-means data set
ag <- aggregate(df1$SpotPriceEUR, list(df1$PriceArea), mean)
# Specifying plot/animation features
p <- ggplot() +
geom_point(data = df1, aes(x=SpotPriceEUR, y = PriceArea, colour = SpotPriceEUR,size=2),show.legend=FALSE) +
geom_point(data = ag, aes(x=ag$x, y = ag$Group.1, size = 1, shape=8),color = "blue",show.legend=FALSE) +
scale_shape_identity() +
scale_colour_continuous(low = "green", high = "red",guide = "colourbar") +
transition_time(Hour) +
scale_alpha(range = c(0,1)) +
guides(alpha = F) +
theme_minimal() +
labs(title = "Date: {frame_time}",x = "Spot Price [EUR]",y = "Price Area")
# Animate
animate(p)
This is what I got:
Hope you can visualize what I am looking for.
All the best,
DW
Bonus: I am also looking for a way to randomly offset the scatter points in y axis.
Take a look at the [enter/exit() functions][1] in gganimate
I think you want something like:
p <- ggplot() +
geom_point(data = df1, aes(x=SpotPriceEUR, y = PriceArea, colour = SpotPriceEUR,size=2),show.legend=FALSE) +
geom_point(data = ag, aes(x=ag$x, y = ag$Group.1, size = 1, shape=8),color = "blue",show.legend=FALSE) +
scale_shape_identity() +
scale_colour_continuous(low = "green", high = "red",guide = "colourbar") +
transition_time(Hour) +
scale_alpha(range = c(0,1)) +
guides(alpha = F) +
theme_minimal() +
labs(title = "Date: {frame_time}",x = "Spot Price [EUR]",y = "Price Area") +
exit_fade()
To add a bit of random variation along the y-axis, you'll want to use geom_jitter(). Try this:
p <- ggplot() +
geom_jitter(data = df1, aes(x=SpotPriceEUR, y = PriceArea, colour = SpotPriceEUR,size=2),show.legend=FALSE, width = 0, height = 0.25) +
geom_point(data = ag, aes(x=ag$x, y = ag$Group.1, size = 1, shape=8),color = "blue",show.legend=FALSE) +
scale_shape_identity() +
scale_colour_continuous(low = "green", high = "red",guide = "colourbar") +
transition_time(Hour) +
scale_alpha(range = c(0,1)) +
guides(alpha = F) +
theme_minimal() +
labs(title = "Date: {frame_time}",x = "Spot Price [EUR]",y = "Price Area") +
exit_fade()
You may have to play around with the width/height parameter in geom_jitter to get exactly the way you want it.
I am trying to change the labels of a legend on a ggplot where I have legends for 2 aes.
With scale_file_manual, it works, but for one of the two legends only. I guess I should use "guides" that I already used to remove the titles and also remove legend for a 3rd aes, but I do not manage to do it.
Do you have a solution?
Here is my code :
p <- ggplot(data, aes(x = Nb))
p + geom_ribbon(aes(ymin = Sandwich.min, ymax = Sandwich.max, fill = 'grey70',alpha=0.8)) +
geom_ribbon(aes(ymin = Assiette.min, ymax = Assiette.max, fill = '#6495ED80',alpha=0.8)) +
geom_line(aes(y = Pizza, col = '#FF7F24')) +
geom_line(aes(y = Sushis, col = '#228B22')) +
labs(title = "Business lunch cost by number of participants",
x = "Number of participants",
y = "Price (Euros)") +
scale_x_continuous(breaks=seq(1,20,1)) +
scale_y_continuous(breaks = seq(0,300,50)) +
theme_light() +
theme(plot.title = element_text(size = 12, hjust = 0.5)) +
guides(alpha = FALSE, colour = guide_legend(" "), fill = guide_legend(" ")) +
scale_fill_manual(
values=c('#6495ED80','grey70'),
labels=c("Assiettes","Sandwiches"))
I am doing a plot of densities, I want to add a legend but is overlapped with the symbol. The code is hereunder:
dfGamma = data.frame(a = rgamma(100,shape = 7.1,rate= 0.0055),
b = rgamma(100, shape = 10,rate= 0.0055),
c = rgamma(100, shape = 7.1,rate= 0.0055))
dfGamma = stack(dfGamma)
p <- ggplot(dfGamma, aes(x = values)) +
stat_density(aes(group = ind, colour = ind),position="identity",geom="line",size=1)+
ggtitle("Gamma distribution")+theme(legend.position="right")+
scale_color_manual(labels = c(expression(paste(alpha,"=7.1 ",beta,"=0.0055")),
expression(paste(alpha,"= 10 ",beta,"=0.0055")),
expression(paste(alpha,"=7.1 ",beta,"=0.0055"))),
values = c('red', 'blue',"green"))
p
the plot is:
The guides option, guide_legend is what you need. You can read more about it in the ggplot reference. Does this help?
p <- ggplot(dfGamma, aes(x = values)) +
stat_density(aes(group = ind, colour = ind),position="identity",geom="line",size=1)+
ggtitle("Gamma distribution")+
theme(legend.position="right") +
scale_color_manual(labels = c(expression(paste(alpha, "=7.1 ", beta, "=0.0055")),
expression(paste(alpha,"= 10 ",beta,"=0.0055")),
expression(paste(alpha,"=7.1 ",beta,"=0.0055"))),
values = c('red', 'blue',"green")) +
guides(colour = guide_legend(label.position = "bottom"))
p
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")