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

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

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

How to delete legend in ggplot with reression model?

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

Two lines appear on my plot when there should be one

I am making a plot in ggplot, and when I add the geom_line() layer it includes 2 lines instead of one. Can anyone help me understand why it's doing this?
Code:
library(ggplot2)
a <- data.frame(SubjectId=c(1:3, 1:3, 1:3, 1:3),
Cycle=c(1,1.1,1.2, 2,2.1,2.2, 3,3.1,3.2, 4,4.1,4.2),
Dose=c(sort(rep(1:3,3)), 3,3,3),
DLT=c("No","No","Yes","No","No","No","No","Yes","Yes","No","Yes","Yes"))
ggplot(aes(x=Cycle, y=Dose, fill=DLT), data = a) +
scale_fill_manual(values = c("white", "black")) +
geom_line(colour="grey20", size=1) +
geom_point(shape=21, size=5) +
xlim(1, 4.5) +
ylim(1, 4) +
ylab("Dose Level") +
theme_classic() +
theme(axis.text =element_text(size=10),
axis.title =element_text(size=12, face="bold", colour="grey20"),
legend.text =element_text(size=10),
legend.title=element_text(size=12, face="bold", colour="grey20"))
I just want one line to go through the points in order of Cycle, but sorting a by Cycle doesn't change the line at all. What am I doing wrong?
put fill=DLT in the geom_point() section, not at the top. Eg:
ggplot(aes(x=Cycle, y=Dose), data = a) +
scale_fill_manual(values = c("white", "darkred")) +
geom_line(colour="grey20", size=1) +
geom_point(shape=23, size=5, aes(fill=DLT)) +
xlim(1, 4.5) +
ylim(1, 4) +
ylab("Dose Level") +
theme_classic() +
theme(axis.text =element_text(size=10),
axis.title =element_text(size=12, face="bold", colour="grey20"),
legend.text =element_text(size=10),
legend.title=element_text(size=12, face="bold", colour="grey20"))

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.

Overlay density of one variable on trend line

I have some data:
dat <- data.frame(x=rnorm(100,100,100),y=rnorm(100,100,100))
I can plot it with a local trend line:
ggplot(dat, aes(x,y)) + stat_smooth()
But I want to overlay a density curve, on the same plot, showing the distribution of x. So just add the previous graph to this one (the y-axis is different, but I only care about relative differences in the density curve anyway):
ggplot(dat, aes(x)) + geom_density()
I know there's stat_binhex() and stat_sum() etc showing where the data falls. There are only a few y values, so what gets plotted by stat_binhex() etc is hard to read.
You can plot a combination of histograms and density curves at both sides of the scatterplot. In the example below I also included a confidence ellipse:
require(ggplot2)
require(gridExtra)
require(devtools)
source_url("https://raw.github.com/low-decarie/FAAV/master/r/stat-ellipse.R") # in order to create a 95% confidence ellipse
htop <- ggplot(data=dat, aes(x=x)) +
geom_histogram(aes(y=..density..), fill = "white", color = "black", binwidth = 2) +
stat_density(colour = "blue", geom="line", size = 1.5, position="identity", show_guide=FALSE) +
scale_x_continuous("x-var", limits = c(-200,400), breaks = c(-200,0,200,400)) +
scale_y_continuous("Density", breaks=c(0.0,0.01,0.02), labels=c(0.0,0.01,0.02)) +
theme_bw() + theme(axis.title.x = element_blank())
blank <- ggplot() + geom_point(aes(1,1), colour="white") +
theme(axis.ticks=element_blank(), panel.background=element_blank(), panel.grid=element_blank(),
axis.text.x=element_blank(), axis.text.y=element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank())
scatter <- ggplot(data=dat, aes(x=x, y=y)) +
geom_point(size = 0.6) + stat_ellipse(level = 0.95, size = 1, color="green") +
scale_x_continuous("x-var", limits = c(-200,400), breaks = c(-200,0,200,400)) +
scale_y_continuous("y-var", limits = c(-200,400), breaks = c(-200,0,200,400)) +
theme_bw()
hright <- ggplot(data=dat, aes(x=y)) +
geom_histogram(aes(y=..density..), fill = "white", color = "black", binwidth = 1) +
stat_density(colour = "red", geom="line", size = 1, position="identity", show_guide=FALSE) +
scale_x_continuous("y-var", limits = c(-200,400), breaks = c(-200,0,200,400)) +
scale_y_continuous("Density", breaks=c(0.0,0.01,0.02), labels=c(0.0,0.01,0.02)) +
coord_flip() + theme_bw() + theme(axis.title.y = element_blank())
grid.arrange(htop, blank, scatter, hright, ncol=2, nrow=2, widths=c(4, 1), heights=c(1, 4))
the result:

Resources