How to delete legend in ggplot with reression model? - r

I'd like to ask how to delete legend in ggplot with regression model.
I already added theme(legend.position = "None")
but the legend cannot be deleted. Could you tell me what I was doing wrong?
Extra question!!
In my current code, how to change the symbol size and shape between N0 and N1? I want more bigger size of 'open circle', and 'closed square' shape.
Many thanks!!!
ggplot(data=x, aes(x=agw, y=pgw)) +
geom_point (data=x, aes(x=agw, y=pgw, color=Nitrogen)) +
stat_smooth(method = 'lm', se=FALSE, color="Black") +
scale_color_manual(values = c("Dark gray","Black")) +
theme(legend.position = "None") +
geom_text(x=30, y=70, label="", size=3.5, col="Black") +
geom_text(x=30, y=60, label="", size=3.5, col="Black") +
scale_x_continuous(breaks = seq(0,80,10),limits = c(0,80)) +
scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
theme_bw() +
theme(panel.grid = element_blank())

This should work in lack of reproducible data. Be careful that functions like theme_bw() use to remove previous theme() settings as mentioned by #Ronald. So it is better to add in the final part of the plot. For shapes, you can enable shape in aes() like this and format with scale_shape_manual() (the numbers inside belong to the shape you want):
library(ggplot2)
#Code
ggplot(data=x, aes(x=agw, y=pgw)) +
geom_point (data=x, aes(x=agw, y=pgw, color=Nitrogen,shape=Nitrogen,size=3)) +
stat_smooth(method = 'lm', se=FALSE, color="Black") +
scale_color_manual(values = c("Dark gray","Black")) +
scale_shape_manual(values = c(1,15))+
geom_text(x=30, y=70, label="", size=3.5, col="Black") +
geom_text(x=30, y=60, label="", size=3.5, col="Black") +
scale_x_continuous(breaks = seq(0,80,10),limits = c(0,80)) +
scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
theme_bw() +
theme(panel.grid = element_blank(),
legend.position = 'none')

For the legend: add the argument show.legend = F inside geom_point. For the different point size: can you give us an example of your dataset? We may need to reshape it.
ggplot(data=x, aes(x=agw, y=pgw)) +
geom_point (data=x, aes(x=agw, y=pgw, color=Nitrogen), show.legend = F) +
stat_smooth(method = 'lm', se=FALSE, color="Black") +
scale_color_manual(values = c("Dark gray","Black")) +
theme(legend.position = "None") +
geom_text(x=30, y=70, label="", size=3.5, col="Black") +
geom_text(x=30, y=60, label="", size=3.5, col="Black") +
scale_x_continuous(breaks = seq(0,80,10),limits = c(0,80)) +
scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
theme_bw() +
theme(panel.grid = element_blank())

Related

Hex colour not plotting correctly in ggplot2 (R) [duplicate]

I am trying to change the colors in a plot manually but
My code with dummydata:
df2=data.frame(y=runif(10), ontopic=c(F,F,F,F,F,T,T,T,T,T))
plot_right <- ggplot(df2, aes(y, fill = ontopic)) + geom_density(alpha = 0.5) +
coord_flip() + theme(legend.position = "none")+
scale_y_continuous(breaks=seq(0, 1, 0.5))+
scale_color_manual(values=c("#999999", "#E69F00"))
plot_right
This returns the standard colors. What do I need to do to change the colors to my manual selection?
Using scale_fill_manual instead of scale_colour_manual should work.
ggplot(df2, aes(y, fill = ontopic, colour= ontopic)) +
geom_density(alpha = 0.5) +
coord_flip() + theme(legend.position = "none")+
scale_y_continuous(breaks=seq(0, 1, 0.5)) +
scale_fill_manual(values=c("#999999", "#E69F00"))

Set variable colors on ggplot2 [duplicate]

I am trying to change the colors in a plot manually but
My code with dummydata:
df2=data.frame(y=runif(10), ontopic=c(F,F,F,F,F,T,T,T,T,T))
plot_right <- ggplot(df2, aes(y, fill = ontopic)) + geom_density(alpha = 0.5) +
coord_flip() + theme(legend.position = "none")+
scale_y_continuous(breaks=seq(0, 1, 0.5))+
scale_color_manual(values=c("#999999", "#E69F00"))
plot_right
This returns the standard colors. What do I need to do to change the colors to my manual selection?
Using scale_fill_manual instead of scale_colour_manual should work.
ggplot(df2, aes(y, fill = ontopic, colour= ontopic)) +
geom_density(alpha = 0.5) +
coord_flip() + theme(legend.position = "none")+
scale_y_continuous(breaks=seq(0, 1, 0.5)) +
scale_fill_manual(values=c("#999999", "#E69F00"))

Change the font size of variable names in ggplot

I am not able to increase the font size of the names of the variables in a graphic realized with ggplot.
I tried to include these codes inside ggplot code, but unsuccessfully :
theme(text = element_text(size=20))
theme(axis.text=element_text(size=20))
theme(axis.title=element_text(size=14))
theme_grey(base_size = 20)
geom_text(size=20)
My code is :
library(ggplot2)
library(reshape2)
dataplot <- read.csv("/Documents/R.csv",header=T,sep=";")
dataPlotMelt <- melt(data = dataplot, id.vars = c("variable"),variable.name = "Method",value.name = "SMD")
varNames <- as.character(dataplot$variable)
dataPlotMelt$variable <- factor(dataPlotMelt$variable,levels = varNames)
ggplot(data=dataPlotMelt,mapping=aes(x=variable,y=SMD,group=Method, color=Method))+
ylab("Standardizedmeandifference(%)")+
xlab("") +
geom_point(aes(shape=Method),size=2) +
geom_hline(yintercept=15,color="black",size=0.1,linetype="dashed") +
geom_hline(yintercept=-15,color="black",size=0.1,linetype="dashed") +
coord_flip() +
theme(axis.text.x=element_blank()) +
scale_y_continuous(breaks=c(-65,-15,15,105)) +
theme_bw() +
theme(legend.text=element_text(size=12)) +
theme(legend.title=element_blank(),legend.key=element_blank()) +
scale_colour_manual(values=c("grey","black"))
I'd like to increase the font size of the names of the variables in the graphic and, besides, increase the text "Standardized mean difference (%)" and remove the vertical line between the yintercept and ybreak on both sides
new graphic
Thank you Richard for giving me the solution.
As you suggested I used theme after theme_bw
I managed to suppress the useless vertical lines as well with the command theme(panel.grid.minor = element_blank())
Here is the new code for ggplot :
ggplot(data = dataPlotMelt, mapping = aes(x = variable, y = SMD,group = Method,
color = Method)) +
ylab("Standardized mean difference (%)") + xlab("") +
geom_point(aes(shape = Method),size=2) +
geom_hline(yintercept = 15, color = "black", size = 0.1, linetype = "dashed") +
geom_hline(yintercept = -15, color = "black", size = 0.1, linetype = "dashed") +
coord_flip() +
theme(axis.text.x = element_blank()) +
scale_y_continuous(breaks=c(-65,-15,0,15,105)) +
theme_bw() + theme(legend.text = element_text(size=13)) +
scale_colour_manual(values= c("grey","black")) +
theme(axis.text.y = element_text(size=12)) +
theme(axis.title.x = element_text(size=13)) +
theme(panel.grid.minor = element_blank()) +
theme(legend.title = element_blank(), legend.key=element_blank())

How to plot a single value on an axis in ggplot2?

I want to create a plot, similar to the one below:
set.seed(8)
xpos<-rep(1:4,2)
sample<-rep(c("One Stage","Two Stage"),each=4)
dat<-data.frame(cbind(xpos,choice,sample))
dat$xpos<-as.integer(dat$xpos)
dat$choice<-(c(.2,.4,.3,.22,.17,.03,.081,.035))
dat$sample<-as.character(dat$sample)
ggplot(data=dat, aes(x=xpos, y=choice, group=sample, shape=factor(sample), colour=factor(sample))) +
geom_line(aes(linetype=factor(sample)))+
geom_point(size=3)+
geom_point(aes(x=1, y=0.5),size=3)+
scale_linetype_manual(values=c("longdash", "dotted"))+
scale_x_continuous("Block", breaks=seq(1,4,1),limits=c(0.8,4.2))+
scale_y_continuous("Choice Rates", breaks=seq(0,1,0.2),limits=c(-0.02,1))+
theme_classic(base_size = b.size)+
labs(title="Model predictions" )+ theme(plot.title = element_text(hjust=0.5))+
theme(legend.title =element_blank(),legend.position=c(.8,.85)) + scale_color_grey(start=0.2, end=0.2)
Here is how the graph looks like:
Now, I want the single point to appear on the Y-Axis.
I know that I should change for this the "scale_x_continuous" limits (from 0.8, to 0), but then I get the following graph, which is not what I want:
What can be done?
Here is the final wanted result:
Thanks to the comments below, I realized how to do it:
ggplot(data=dat, aes(x=xpos, y=choice, group=sample, shape=factor(sample),
colour=factor(sample))) +
geom_line(aes(linetype=factor(sample)))+
geom_point(size=3)+
geom_point(aes(x=0.8, y=0.5),size=3)+
scale_linetype_manual(values=c("longdash", "dotted"))+
scale_x_continuous("Block", breaks=seq(1,4,1),limits=c(0.8,4.2),expand=c(0,0))+
coord_cartesian(clip = 'off') +
scale_y_continuous("Checking Rates", breaks=seq(0,1,0.2),limits=c(-0.02,1))+
theme_classic(base_size = b.size)+
labs(title="Model predictions" )+ theme(plot.title = element_text(hjust=0.5))+
theme(legend.title =element_blank(),legend.position=c(.8,.85)) + scale_color_grey(start=0.2, end=0.2)
You should us expand = c(0,0)) in the scale_x_continuous, and add coord_cartesian(clip = 'off') so the points don't get clipped (where half of the shape falls outside the plotting area).
ggplot(data=dat, aes(x=xpos, y=choice, group=sample, shape=factor(sample), colour=factor(sample))) +
geom_line(aes(linetype=factor(sample)))+
geom_point(size=3)+
geom_point(aes(x=1, y=0.5),size=3)+
scale_linetype_manual(values=c("longdash", "dotted"))+
scale_x_continuous("Block", breaks=seq(1,4,1),
limits=c(1,4.2), expand = c(0,0))+
coord_cartesian(clip = 'off') +
scale_y_continuous("Choice Rates", breaks=seq(0,1,0.2),limits=c(-0.02,1))+
labs(title="Model predictions" )+ theme(plot.title = element_text(hjust=0.5))+
theme(legend.title =element_blank(),legend.position=c(.8,.85)) +
scale_color_grey(start=0.2, end=0.2) +
theme_classic()

why line cutting the points is showing same color in legend for different plots in r ggplot

I am mapping aesthetic to a shape and created a manual shape scale. But the line connecting the points showing same color in the legend for two different lines.
p <- ggplot(data=data_read) + stat_summary(colour="blue", fun.y=mean,
geom="line", size=1.0, aes(x=factor(x), y=A, group=1), show.legend=T) +
stat_summary(fun.y=mean, geom="point", aes(x=factor(x), y=A, shape="A"),
size=5,col="black") +
stat_summary(colour="green", fun.y=mean, geom="line", size=1.0, aes(x=factor(x),
y=B, group=1), show.legend=T) +
stat_summary(fun.y=mean, geom="point", aes(x=factor(x), y=B, shape="B"),
size=3, col="black") +
scale_shape_manual("title", values=c("A"="\U0394", "B"="*"))
p + theme(legend.position = c(0.5,0.5),
legend.text = element_text(size = 15, colour = "black"),
legend.title = element_text(size = 15, colour = "black")) +
coord_cartesian(ylim = c(10,30), expand = FALSE) +
expand_limits(x=0, y=10)
My data file is here.

Resources