Why ggplot2 legend not show in the graph [duplicate] - r

This question already has answers here:
Add legend to ggplot2 line plot
(4 answers)
Closed 2 years ago.
I use ggplot to scatterplot 2 datasets and want to show the legend in the top left. I tried some code but didn't work. I am not sure why this happened.
ggplot(mf, aes(log10(mf[,2]),mf[,1]))
+ ggtitle("Plot")
+ geom_point(color = "blue") + theme(plot.margin = unit(c(1,2,1,1), "cm"))
+ xlab("xxx") + ylab("yyy")
+ theme(plot.title = element_text(size=18,hjust = 0.5, vjust=4))
+ geom_point(data=mf2,aes(log10(mf2[,2]),mf2[,1]),color="red")
+ theme(axis.title.x = element_text(size = rel(1.3)))
+ theme(axis.title.y = element_text(size = rel(1.3)))
+ scale_color_discrete(name = "Dataset",labels = c("Dataset 1", "Dataset 2"))

Since values were not provided, I have used my own values for the demonstration purpose.
mf is a dataframe with log and val as it's column.
You need to put the color parameter inside the aesthetics. This will result in the mapping of colors for the legend. After that you can manually scale the color to get any color you desire.
you can use the below code to get the desired result.
ggplot(mf, aes(val,log))+
geom_point(aes(color = "Dataset1"))+
geom_point(data=mf2,aes(color="Dataset2"))+
labs(colour="Datasets",x="xxx",y="yyy")+
theme(legend.position = c(0, 1),legend.justification = c(0, 1))+
scale_color_manual(values = c("blue","red"))

Related

how to show all mean values in the boxplot with ggplot2? [duplicate]

This question already has answers here:
Add mean to grouped box plot in R with ggplot2
(2 answers)
Closed 1 year ago.
I am trying to add the mean values (as shown in red dots in the plot below) in the boxplot with ggplot2. I used stat_summary to add mean values.
However, the following plot is not the exact one that I am looking for. What I'd like to get is to show two mean values for both Y (blue box) and N (red box), not one mean value for both.
Here is my code.
ggplot(data = df.08.long,
aes(x = TMT_signals, y = as.numeric(TMT_Intensities), fill = `probe.Mod.or.not(Y/N)`)) +
geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=20, size=5, color="red", fill="red") +
coord_cartesian(
xlim = NULL,
ylim = c(0, 2e4),
expand = TRUE,
default = FALSE,
clip = "on")
theme_classic() +
theme(axis.title=element_text(size=8),
axis.text=element_text(size=10),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
Does anyone know how to solve this problem?
Thanks so much for any help!
mtcars example
Code
mtcars %>%
ggplot(aes(as.factor(vs),drat, fill = as.factor(am)))+
geom_boxplot()+
stat_summary(
fun=mean,
geom="point",
shape=21,
size=5,
#Define the aesthetic inside stat_summary
aes(fill = as.factor(am)),
position = position_dodge2(width = .75),
show.legend = FALSE
)
Output

ggplot2 geom_jitterdodge points and with overlayed dodged boxplots: I want to preserve color in points but force boxplots to be black

Using ggplot2, I want to geom_jitterdodge a swarm of points with overlayed dodged boxplots. The trick is that I want the boxplots to be black, not colored like the points. The point plot looks like this:
It's easy enough to get boxplots in place:
The code for that looks like this:
D_cohort1 %>%
filter(!is.na(pssa_ela_code)) %>%
ggplot(aes(x=timepoint,
y=dibels_lnf,
color=pssa_ela_code)) +
geom_point(alpha=1/6, size=2, width=1/3, height=0,
position=position_jitterdodge()) +
geom_boxplot(fill=NA, outlier.shape=NA,
position=position_dodge2(padding=.3)) +
facet_grid(rows=vars(school_type)) +
guides(colour = guide_legend(override.aes = list(alpha=1))) +
labs(title="Figure A.1: DIBELS LNF Scores at each Timepoint") +
theme_cowplot() +
theme(plot.background=element_rect(fill="aliceblue"),
panel.border=element_rect(color="black", fill=NA),
legend.position = c(.85,.87),
legend.text = element_text(size = rel(.7)))
For visibilities sake, I want the boxplot lines to be black, but I can't quite figure out how to get there. Closest I've come is this (same as before but for the call to geom_boxplot():
D_cohort1 %>%
filter(!is.na(pssa_ela_code)) %>%
ggplot(aes(x=timepoint,
y=dibels_lnf,
color=pssa_ela_code)) +
geom_point(alpha=1/6, size=2, width=1/3, height=0,
position=position_jitterdodge()) +
geom_boxplot(aes(color=NULL, group=fct_cross(timepoint, pssa_ela_code)),
fill=NA, outlier.shape=NA,
position=position_dodge2(padding=.3)) +
facet_grid(rows=vars(school_type)) +
guides(colour = guide_legend(override.aes = list(alpha=1))) +
labs(title="Figure A.1: DIBELS LNF Scores at each Timepoint") +
theme_cowplot() +
theme(plot.background=element_rect(fill="aliceblue"),
panel.border=element_rect(color="black", fill=NA),
legend.position = c(.85,.87),
legend.text = element_text(size = rel(.7)))
That gets the color effect I want, but positions the boxplots incorrectly. Shown here:
How can I achieve the effect I want: correctly positioned, black boxplots over colored points?
Ok. I slept on it and was able to come up with a solution this morning. The effect I want is shown below. The code used to get there is this:
D_cohort1 %>%
filter(!is.na(pssa_ela_code)) %>%
ggplot(aes(x=timepoint,
y=dibels_lnf,
color=pssa_ela_code)) +
geom_point(alpha=1/6, size=2, width=1/3, height=0,
position=position_jitterdodge()) +
geom_boxplot(aes(color=NULL, fill=pssa_ela_code),
outlier.shape=NA, alpha=0,
position=position_dodge2(padding=.3)) +
facet_grid(rows=vars(school_type)) +
guides(colour = guide_legend(override.aes = list(alpha=1))) +
labs(title="Figure A.1: DIBELS LNF Scores at each Timepoint") +
theme_cowplot() +
theme(plot.background=element_rect(fill="aliceblue"),
panel.border=element_rect(color="black", fill=NA),
legend.position = c(.85,.87),
legend.text = element_text(size = rel(.7)))
It's the same as before but for the call to geom_boxplot(). It took over-riding the color aesthetic and setting fill. Then, alpha=0 makes the fill fully transparent, which is what I want.

Order data on ggplot [duplicate]

This question already has answers here:
Reorder bars in geom_bar ggplot2 by value
(3 answers)
Closed 3 years ago.
I currently have a ggplot however it is shown in alphabetical order, I want the graph to show the most 'important score' first and order in descending order. See image of plot attached and code.
library(ggplot2)
ggplot(data= VIMP, aes(x=(VIMP$Y),y=VIMP$X)) +
geom_bar(position="dodge",stat="identity",width = 0, color =
"black") +
coord_flip() + geom_point(color='skyblue') +
xlab("Variables")+ylab(" Importance Score")+
ggtitle("Variable Importance") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.background = element_rect(fill = 'white', colour =
'black'))
To solve this problem you might use the library(forcats) package. Forcats is a package that was made to deal with factors in R.
This code might work for you.
VIMP <- VIMP %>%
mutate(Y = forcats::fct_reorder(Y, X)) ##reorder the Y variable based on X, it's also possible to change to a descending order using desc(X).
ggplot(data= VIMP, aes(x=(VIMP$Y),y=VIMP$X)) +
geom_bar(position="dodge",stat="identity",width = 0, color =
"black") +
coord_flip() + geom_point(color='skyblue') +
xlab("Variables")+ylab(" Importance Score")+
ggtitle("Variable Importance") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.background = element_rect(fill = 'white', colour = 'black'))

Manually change order of y axis items on complicated stacked bar chart in ggplot2

I've been stuck on an issue and can't find a solution. I've tried many suggestions on Stack Overflow and elsewhere about manually ordering a stacked bar chart, since that should be a pretty simple fix, but those suggestions don't work with the huge complicated mess of code I plucked from many places. My only issue is y-axis item ordering.
I'm making a series of stacked bar charts, and ggplot2 changes the ordering of the items on the y-axis depending on which dataframe I am trying to plot. I'm trying to make 39 of these plots and want them to all have the same ordering. I think ggplot2 only wants to plot them in ascending order of their numeric mean or something, but I'd like all of the bar charts to first display the group "Bird Advocates" and then "Cat Advocates." (This is also the order they appear in my data frame, but that ordering is lost at the coord_flip() point in plotting.)
I think that taking the data frame through so many changes is why I can't just add something simple at the end or use the reorder() function. Adding things into aes() also doesn't work, since the stacked bar chart I'm creating seems to depend on those items being exactly a certain way.
Here's one of my data frames where ggplot2 is ordering my y-axis items incorrectly, plotting "Cat Advocates" before "Bird Advocates":
Group,Strongly Opposed,Opposed,Slightly Opposed,Neutral,Slightly Support,Support,Strongly Support
Bird Advocates,0.005473026,0.010946052,0.012509773,0.058639562,0.071149335,0.31118061,0.530101642
Cat Advocates,0.04491726,0.07013396,0.03624901,0.23719464,0.09141056,0.23404255,0.28605201
And here's all the code that takes that and turns it into a plot:
library(ggplot2)
library(reshape2)
library(plotly)
#Importing data from a .csv file
data <- read.csv("data.csv", header=TRUE)
data$s.Strongly.Opposed <- 0-data$Strongly.Opposed-data$Opposed-data$Slightly.Opposed-.5*data$Neutral
data$s.Opposed <- 0-data$Opposed-data$Slightly.Opposed-.5*data$Neutral
data$s.Slightly.Opposed <- 0-data$Slightly.Opposed-.5*data$Neutral
data$s.Neutral <- 0-.5*data$Neutral
data$s.Slightly.Support <- 0+.5*data$Neutral
data$s.Support <- 0+data$Slightly.Support+.5*data$Neutral
data$s.Strongly.Support <- 0+data$Support+data$Slightly.Support+.5*data$Neutral
#to percents
data[,2:15]<-data[,2:15]*100
#melting
mdfr <- melt(data, id=c("Group"))
mdfr<-cbind(mdfr[1:14,],mdfr[15:28,3])
colnames(mdfr)<-c("Group","variable","value","start")
#remove dot in level names
mylevels<-c("Strongly Opposed","Opposed","Slightly Opposed","Neutral","Slightly Support","Support","Strongly Support")
mdfr$variable<-droplevels(mdfr$variable)
levels(mdfr$variable)<-mylevels
pal<-c("#bd7523", "#e9aa61", "#f6d1a7", "#999999", "#c8cbc0", "#65806d", "#334e3b")
ggplot(data=mdfr) +
geom_segment(aes(x = Group, y = start, xend = Group, yend = start+value, colour = variable,
text=paste("Group: ",Group,"<br>Percent: ",value,"%")), size = 5) +
geom_hline(yintercept = 0, color =c("#646464")) +
coord_flip() +
theme(legend.position="top") +
theme(legend.key.width=unit(0.5,"cm")) +
guides(col = guide_legend(ncol = 12)) + #has 7 real columns, using to adjust legend position
scale_color_manual("Response", labels = mylevels, values = pal, guide="legend") +
theme(legend.title = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(axis.title.y = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(axis.text.x = element_blank()) +
theme(legend.key = element_rect(fill = "white")) +
scale_y_continuous(breaks=seq(-100,100,100), limits=c(-100,100)) +
theme(panel.background = element_rect(fill = "#ffffff"),
panel.grid.major = element_line(colour = "#CBCBCB"))
The plot:
I think this works, you may need to play around with the axis limits/breaks:
library(dplyr)
mdfr <- mdfr %>%
mutate(group_n = as.integer(case_when(Group == "Bird Advocates" ~ 2,
Group == "Cat Advocates" ~ 1)))
ggplot(data=mdfr) +
geom_segment(aes(x = group_n, y = start, xend = group_n, yend = start + value, colour = variable,
text=paste("Group: ",Group,"<br>Percent: ",value,"%")), size = 5) +
scale_x_continuous(limits = c(0,3), breaks = c(1, 2), labels = c("Cat", "Bird")) +
geom_hline(yintercept = 0, color =c("#646464")) +
theme(legend.position="top") +
theme(legend.key.width=unit(0.5,"cm")) +
coord_flip() +
guides(col = guide_legend(ncol = 12)) + #has 7 real columns, using to adjust legend position
scale_color_manual("Response", labels = mylevels, values = pal, guide="legend") +
theme(legend.title = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(axis.title.y = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(axis.text.x = element_blank()) +
theme(legend.key = element_rect(fill = "white"))+
scale_y_continuous(breaks=seq(-100,100,100), limits=c(-100,100)) +
theme(panel.background = element_rect(fill = "#ffffff"),
panel.grid.major = element_line(colour = "#CBCBCB"))
produces this plot:
You want to factor the 'Group' variable in the order by which you want the bars to appear.
mdfr$Group <- factor(mdfr$Group, levels = c("Bird Advocates", "Cat Advocates")

Removing "a" symbol from colour legend in ggplot2 [duplicate]

This question already has answers here:
Remove 'a' from legend when using aesthetics and geom_text
(6 answers)
Closed 5 years ago.
I keep getting this a on my colour legend when I make this graph in GGPLOT2.
ggplot(sher_ei_si, aes(SI, EI, shape = crop, label = treatment, colour =
management)) +
geom_point() +
geom_text_repel(aes(SI, EI)) +
xlim(0, 100) +
ylim(0, 100) +
labs(x = "Structure", y = "Enrichment", shape = "Crop", colour =
"Management") +
geom_vline(xintercept = 50) +
geom_hline(yintercept = 50) +
scale_colour_manual(values = c("grey0", "grey60")
Plot showing a under colour legend
For exact output generation, please provide the input data.
You can use show.legend = FALSE to exclude the a symbol from your example:
geom_text_repel(aes(SI, EI), show.legend = FALSE)

Resources