Cannot change ggplot boxplot legend name - r

I am using this code:
library(ggplot2)
library(reshape)
mtcars <- melt(mtcars, id="am")
mtcars$am <- as.character(mtcars$am)
p <- ggplot(mtcars, aes(x = variable, y = value)) +
geom_boxplot(aes(color = am), width = 0.4, size = 0.4, position = position_dodge(0.6),
outlier.shape=NA, notch=T) +
scale_color_manual(values = c("red", "blue")) +
guides(fill = guide_legend(reverse=TRUE)) +
scale_fill_discrete(name="No name", labels=c("A", "B")) + #Why does this line not work?
coord_flip()
p
Why do the legend name and variable names not change? How can I change them?

Does this solve your problem?
library(ggplot2)
library(reshape)
mtcars <- melt(mtcars, id="am")
mtcars$am <- as.character(mtcars$am)
p <- ggplot(mtcars, aes(x = variable, y = value)) +
geom_boxplot(aes(color = am), width = 0.4, size = 0.4, position = position_dodge(0.6),
outlier.shape=NA, notch=T) +
scale_color_manual(values = c("red", "blue"),
name="No name", labels=c("A", "B")) +
guides(fill = guide_legend(reverse=TRUE)) +
coord_flip()
p
The issue is that you don't use "fill" in the plot (you use "color"), so adjusting the "fill" scale doesn't have any effect.
Also, change guides(fill = guide_legend(reverse=TRUE)) to guides(color = guide_legend(reverse=TRUE)) to reverse the legend order.

Related

geom_smooth and geom_point do not have legends

I want to create a plot that looks like this:
x=1:20
y=sample(20)
df <- tibble(x=x,y=y)
ggplot(df,aes(x,y))+
geom_smooth()+
geom_point()
But the codes unabled to show legends.
Could anyone help me, thanks!
I'm perhaps being a bit over-literal in your requirements, but you could do:
tibble(x = 1:20, y = sample(20)) %>%
ggplot(aes(x, y)) +
geom_smooth(aes(linetype = "line")) +
geom_point(aes(shape = "point"), color = "red", size = 3) +
theme_gray(base_size = 20) +
theme(legend.position = c(0.75, 0.9),
legend.background = element_blank()) +
labs(shape = NULL, linetype = NULL)
You could set them inside their aes() as variables:
x <- 1:20
y <- sample(20)
library(ggplot2)
library(dplyr)
df <- tibble(x=x,y=y)
ggplot(df,aes(x,y))+
geom_smooth(aes(color = "line"))+
geom_point(aes(color = "point"))+
scale_color_manual(values = c("blue","red"))
With ggnewscale you could try:
library(tibble)
library(ggplot2)
library(ggnewscale)
x=1:20
y=sample(20)
df1 <- tibble(x=x, y=y)
ggplot(df1, aes(x, y))+
geom_smooth(aes(colour = "line"))+
scale_colour_manual(values = "black") +
labs(colour = NULL)+
new_scale_colour()+
geom_point(aes(colour = "point"))+
scale_colour_manual(values = "red")+
labs(colour = NULL)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Created on 2022-11-27 with reprex v2.0.2

how to remove inclined lines added to legend?

How to remove inclined lines added to legend? And also the dots on the yellow and gray... Why is it happening?
library(ggplot2)
x <- seq(from = 1, to = 10, by = 1)
df = data.frame(x=x, y=x^2, v=2*x)
df2 = data.frame(x=x, y=x^2-5*x-10)
ggplot(df, aes(x, y)) +
geom_point(aes(size = v)) +
theme_classic() +
scale_size("blabla") +
geom_point(data=df2, aes(x, y, color = "blue")) +
geom_line(data=df2, aes(x, y, color = "blue")) +
geom_hline(aes(color="gray",yintercept=25)) +
geom_abline(aes(color="yellow", intercept=0, slope=1)) +
scale_color_manual(values = c("blue","gray","yellow"), labels = c("nanana","hhh","abab"), name = "other")
That's the legend for the color aesthetic and it tries to combine all the needed information for geom_point, geom_line, geom_hline, and geom_abline. To get rid of the lines, we instead need
geom_abline(aes(color = "yellow", intercept = 0, slope = 1), show.legend = FALSE)
while for the dots we have to add
guides(color = guide_legend(override.aes = list(shape = c(19, NA, NA))))
This gives

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)

Add item to legend by theme options

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

How to show abline of geom_abline in legend

in the following sample data , how can i display the abline ( i e the red color line) in legend with y.
my code and data:
x<-c(1990,1991,1992,1993,1994,1995)
y<-c(400,500,465,450,550,555)
df<-data.frame(x,y)
df
plot1<- ggplot(df, aes(x)) +
geom_line(size=0.5,lty="dashed", aes(y=y),color="Blue") +
geom_abline(aes(slope=-0.62,intercept=1670,colour="break"),size=0.9)+
geom_point(aes(y=y,shape="y"),size=4, color="Gray24",fill="Green")
plot1
.
what i got is the below image. i need the red line to be displayed in legend
You can use the show_guide=TRUE argument:
plot1<- ggplot(df, aes(x)) +
geom_line(size=0.5,lty="dashed", aes(y=y),color="Blue") +
geom_abline(aes(slope=-0.62,intercept=1670,colour="break"), size=0.9,show_guide = TRUE)+
geom_point(aes(y=y,shape="y"),size=4, color="Gray24",fill="Green")
You'll probably need to change the labels in the legend, but you should be able to do that with theme.
Edit: To remove the slash from the legend, you can use guides and override.aes:
plot1 <- ggplot(df, aes(x, y)) +
geom_point(aes(shape = "y"), size = 4, color = "Gray24", lty = 0) +
geom_line(size = 0.5, lty = "dashed", color = "Blue") +
geom_abline(aes(slope = -0.62, intercept = 1670, colour = "break"), size = 0.9,
show_guide = TRUE) +
guides(shape = guide_legend(override.aes = list(linetype = 0)))

Resources