Add item to legend by theme options - r

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")

Related

R ggplot2 add a legend

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?

R: How can I make scatter points (geom_point) fade by decreasing in size in animation?

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.

create a manual legend for density plots in R (ggplot2)

I want to add a legend to my graph. All solutions I found online use scale_color_manual - but it's not working for me. Where is the legend?
Here is my code:
library(ggplot2)
ggplot() +
geom_density(aes(x = rnorm(100)), color = 'red') +
geom_density(aes(x = rnorm(100)), color = 'blue') +
xlab("Age") + ylab("Density") + ggtitle('Age Densities')
theme(legend.position = 'right') +
scale_color_manual(labels = c('first', 'second'), values = c('red', 'blue'))
If for some reason you absolutely need the two geoms to take on different data sources, move the color = XXX portion inside aes() for each, then define the colors manually using a named vector:
ggplot() +
geom_density(aes(x = rnorm(100), color = 'first')) +
geom_density(aes(x = rnorm(100), color = 'second')) +
xlab("Age") + ylab("Density") + ggtitle('Age Densities') +
theme(legend.position = 'right') +
scale_color_manual(values = c('first' = 'red', 'second' = 'blue'))
Your data are not formatted correctly and you are basically creating two separate plots on a common "canvas", please see the code below (creation of the df is the crucial part):
library(ggplot2)
df <- data.frame(
x = c(rnorm(100), runif(100)),
col = c(rep('blue', 100), rep('red', 100))
)
ggplot(df) +
geom_density(aes(x = x, color = col)) +
xlab("Age") + ylab("Density") + ggtitle('Age Densities') +
theme(legend.position = 'right') +
scale_color_manual(labels = c('first', 'second'), values = c('red', 'blue'))

Aesthetics must be either length 1 or the same as the data (1): x, y, label

I'm working on some data on party polarization (something like this) and used geom_dumbbell from ggalt and ggplot2. I keep getting the same aes error and other solutions in the forum did not address this as effectively. This is my sample data.
df <- data_frame(policy=c("Not enough restrictions on gun ownership", "Climate change is an immediate threat", "Abortion should be illegal"),
Democrats=c(0.54, 0.82, 0.30),
Republicans=c(0.23, 0.38, 0.40),
diff=sprintf("+%d", as.integer((Democrats-Republicans)*100)))
I wanted to keep order of the plot, so converted policy to factor and wanted % to be shown only on the first line.
df <- arrange(df, desc(diff))
df$policy <- factor(df$policy, levels=rev(df$policy))
percent_first <- function(x) {
x <- sprintf("%d%%", round(x*100))
x[2:length(x)] <- sub("%$", "", x[2:length(x)])
x
}
Then I used ggplot that rendered something close to what I wanted.
gg2 <- ggplot()
gg2 <- gg + geom_segment(data = df, aes(y=country, yend=country, x=0, xend=1), color = "#b2b2b2", size = 0.15)
# making the dumbbell
gg2 <- gg + geom_dumbbell(data=df, aes(y=country, x=Democrats, xend=Republicans),
size=1.5, color = "#B2B2B2", point.size.l=3, point.size.r=3,
point.color.l = "#9FB059", point.color.r = "#EDAE52")
I then wanted the dumbbell to read Democrat and Republican on top to label the two points (like this). This is where I get the error.
gg2 <- gg + geom_text(data=filter(df, country=="Government will not control gun violence"),
aes(x=Democrats, y=country, label="Democrats"),
color="#9fb059", size=3, vjust=-2, fontface="bold", family="Calibri")
gg2 <- gg + geom_text(data=filter(df, country=="Government will not control gun violence"),
aes(x=Republicans, y=country, label="Republicans"),
color="#edae52", size=3, vjust=-2, fontface="bold", family="Calibri")
Any thoughts on what I might be doing wrong?
I think it would be easier to build your own "dumbbells" with geom_segment() and geom_point(). Working with your df and changing the variable refences "country" to "policy":
library(tidyverse)
# gather data into long form to make ggplot happy
df2 <- gather(df,"party", "value", Democrats:Republicans)
ggplot(data = df2, aes(y = policy, x = value, color = party)) +
# our dumbell
geom_path(aes(group = policy), color = "#b2b2b2", size = 2) +
geom_point(size = 7, show.legend = FALSE) +
# the text labels
geom_text(aes(label = party), vjust = -1.5) + # use vjust to shift text up to no overlap
scale_color_manual(values = c("Democrats" = "blue", "Republicans" = "red")) + # named vector to map colors to values in df2
scale_x_continuous(limits = c(0,1), labels = scales::percent) # use library(scales) nice math instead of pasting
Produces this plot:
Which has some overlapping labels. I think you could avoid that if you use just the first letter of party like this:
ggplot(data = df2, aes(y = policy, x = value, color = party)) +
geom_path(aes(group = policy), color = "#b2b2b2", size = 2) +
geom_point(size = 7, show.legend = FALSE) +
geom_text(aes(label = gsub("^(\\D).*", "\\1", party)), vjust = -1.5) + # just the first letter instead
scale_color_manual(values = c("Democrats" = "blue", "Republicans" = "red"),
guide = "none") +
scale_x_continuous(limits = c(0,1), labels = scales::percent)
Only label the top issue with names:
ggplot(data = df2, aes(y = policy, x = value, color = party)) +
geom_path(aes(group = policy), color = "#b2b2b2", size = 2) +
geom_point(size = 7, show.legend = FALSE) +
geom_text(data = filter(df2, policy == "Not enough restrictions on gun ownership"),
aes(label = party), vjust = -1.5) +
scale_color_manual(values = c("Democrats" = "blue", "Republicans" = "red")) +
scale_x_continuous(limits = c(0,1), labels = scales::percent)

Only display specific labels of a ggplot legend

I have some data points, and I want to single out some of the points in my visualization. I would do it like this:
df = data.frame(
x = 1:4, y = 1:4,
special = c('normal', 'normal', 'normal', 'special')
)
ggplot(df) +
geom_point(aes(x, y, color = special)) +
scale_color_manual(values = c('red', 'black')) +
labs(color = "") +
theme_bw()
My issue here is that the black points are very self explanatory and don't need a label. I want just the red "special" label to appear. Is there a way I can hide the "normal" label?
If you are open to having any color other than red:
ggplot(df) +
geom_point(aes(x, y, color = special)) + scale_size(guide = "none") +
scale_color_discrete(breaks="special") + labs(color = "") +
theme_bw()
EDIT :
cols <- c("normal" = "black","special" = "red")
gg <- ggplot(df) + geom_point(aes(x, y, color = special)) + labs(color = "") + theme_bw()
gg + scale_colour_manual(values = cols, limits = "special")

Resources