Related
Now, I'm making some hypothesis graph and I made this graph.
x<- c(1,2,3,4,5,6,7,8,9,10,11)
y<- c(100,90,80,70,60,50,40,30,20,10,1)
a<- c(1,2,3,4,5,6,7,8,9,10)
b<- c(1,4,9,16,25,36,49,64,81,100)
dataA<- data.frame (x,y)
dataB<- data.frame (a,b)
geom_line(data=dataA, aes(x=x, y=y), col="Dark red", size=1) +
geom_line(data=dataB, aes(x=a, y=b), col="Dark blue", size=1) +
scale_x_continuous(breaks = seq(0,12,1), limits = c(0,12)) +
scale_y_continuous(breaks = seq(0,120,10), limits = c(0,120)) +
geom_hline(yintercept=70, linetype="dashed", color = "Black", size=1) +
geom_hline(yintercept=50, linetype="dashed", color = "Black", size=1) +
#geom_text(aes(fontface=6), x=11, y=110, label=paste("% distal\n","grains"), size=6, col="Dark blue") +
geom_text(aes(fontface=6), x=10, y=75, label="AGW (90th percentile)", size=5, col="Black") +
geom_text(aes(fontface=6), x=10, y=55, label="AGW (10th percentile)", size=5, col="Black") +
xlab(bquote('x ('~m^2*')')) +
ylab(bquote('y (mg '~grain^-1*')')) +
theme(axis.title = element_text (face = "plain", size = 18, color = "black"),
axis.text.x = element_blank(), #element_blank()) element_text(size= 14)
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.line = element_line(size = 0.5, colour = "black"))+
windows(width=5.5, height=5)
As an alternatives, I need to present such as below graph, but I don't know how to draw an inclined line, starting a specific point of y-axis. Also, I would like to add a text on the line in a parallel position. Could you tell me how I can do this?
Many thanks!!
Here is a concrete example based on #Waldi 's comment:
library(ggplot2)
# Change line to what you want
line <- data.frame(x = c(10,30), y = c(300,50))
line = lm(formula = line$y ~ line$x)$coefficients
# ratio required for unequal axis scales
ratio <- 1/15
# get angle of the line
angle <- atan(line[2] * ratio) * 180 / pi
# plot it
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point() +
geom_abline(slope = line[2], intercept = line[1], color = "blue") +
coord_equal(ratio = ratio) +
annotate(
geom = "text",
x = 20,
y = 20 * line[2] + line[1],
label = "My Line",
color = "blue",
angle = angle,
vjust = -1 # offset so text isn't directly on the line
)
I've been using ggplot2 for long, but never experienced this issue. I am representing confidence intervals of some regressions. However, I decided to manually control the ylim(). I realized that those areas which exceed the y limits are broken. See this picture:
The red regression on the right contains a very wide CLs. As you can see there is a gap in there as its highest point is outside ylim range.
This is the code I used:
ggplot(dataset, aes(x=variable, y=value, fill=Species, colour=Species, linetype = Species)) +
geom_smooth(method="lm", formula= y~poly(x,3), level=0.95, alpha=0.2) +
xlab("A") +
ylab("B") +
ylim(0, 30) +
theme(axis.text.x = element_text(angle = 0, hjust = 0.5, size = 10),
panel.background = element_blank(),
legend.position='bottom',
panel.grid.major = element_line(colour="azure2"),
axis.line = element_line(colour = "black",
size = 0.15, linetype = "solid")) +
scale_x_continuous(breaks=seq(1, 10, 1), limits=c(1, 10)) +
scale_color_manual(values=c("coral4", "coral1", "darkolivegreen3", "darkgoldenrod4", "darkgoldenrod2", "deepskyblue3", "darkorchid3")) +
scale_fill_manual(values=c("coral4", "coral1", "darkolivegreen3", "darkgoldenrod4", "darkgoldenrod2", "deepskyblue3", "darkorchid3")) +
scale_linetype_manual(values=c(1,1,1,3,3,2,2))
I would like to keep these y limits. I used coord_cartesian with no success. Can anybody help me?
coord_cartesian should work, but you have to remove the ylim()
Some data
set.seed(1)
df <- data_frame(x = -5:5, y = rnorm(11, x^2, 5))
Replicating your problem
ggplot(df, aes(x, y)) +
geom_smooth() +
ylim(-1, NA)
With coord_cartesian
ggplot(df, aes(x, y)) +
geom_smooth() +
coord_cartesian(ylim = c(-1, 40))
So I have been struggling to get a correct legend onto this plot. My end goal is a legend that shows both line colours and point colours. I can get the lines in the legend working ok but was wondering how I also get points too (and includes the shape and colour of points)? Any work around or advice much appreciated!
Tempx<-c(20, 22, 24, 26, 28, 30, 32, 34)
Tempx<-rep(Tempx,2)
Temp<-rnorm(1000,28,2)
var1<-rnorm(1000,25,5)
Data<-data.frame(Temp,var1)
Temp<-rnorm(1000,28,2)
var1<-rnorm(1000,12,5)
Data2<-data.frame(Temp,var1)
a1<-1.05*Tempx
a2<-0.5*Tempx
a1low<-0.95*Tempx
a1high<-1.15*Tempx
a2low<-0.4*Tempx
a2high<-0.6*Tempx
plot1<-ggplot(NULL, aes(Temp, var1)) +
geom_point(data = Data, colour="grey60", size=1.5, shape=1, show.legend=TRUE) +
geom_point(data = Data2, shape= 16, size=1, show.legend=TRUE) +
geom_line(aes(x=Tempx,y=a1, colour="grey50"), size=1.75, show.legend=TRUE) +
geom_line(aes(x=Tempx,y=a2, colour="black"), size=1.75, show.legend=TRUE) +
geom_line(aes(x=Tempx,y=a1low), colour="grey50",size=1.25, linetype="longdash") +
geom_line(aes(x=Tempx,y=a1high), colour="grey50",size=1.25, linetype="longdash") +
geom_line(aes(x=Tempx,y=a2low), colour="black",size=1.25, linetype="longdash") +
geom_line(aes(x=Tempx,y=a2high), colour="black",size=1.25, linetype="longdash") +
scale_shape_manual(values=c(1, 16)) +
scale_color_manual(labels = c("low", "high"), values=c('black','grey50')) +
theme_bw()+
theme(axis.line.x = element_line(colour = "black"),
axis.line.y = element_line(colour = "black"),
axis.text.x = element_text(margin=unit(c(0.4,0.4,0.4,0.4), "cm")),
axis.text.y = element_text(margin=unit(c(0.4,0.4,0.4,0.4), "cm")),
axis.title.x = element_text(margin = margin(t = -6)),
axis.title.y = element_text(margin = margin(t = -6)),
axis.text=element_text(size=13),
text = element_text(size=14),
plot.title = element_text(size=16, hjust=0),
axis.ticks.length = unit(-0.1,"cm"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
scale_x_continuous(name = "Temperature", breaks = c(20, 22, 24, 26, 28, 30, 32, 34), expand=c(0,0), limits=c(20,34))+
scale_y_continuous(name = "Variable 1", expand = c(0, 0), limits = c(0, 40)) +
ggtitle("Title")
plot1
You can shorten the code considerably by transforming your data from wide to long format. Then you need only one call to geom_point and geom_line Below is an example. dat1 and dat2 contain the points and main lines, respectively. dat1 is already in long format. We convert dat2 to dat2longas well. I've put dat3long in a separate data frame, as we don't need to map these values to a legend-generating aesthetic. Thus, I've used a separate call to geom_line for these lines and I've hard-coded the colors.
library(tidyverse)
# Create long data frame for point data
dat1 = bind_rows(list(a1=Data, a2=Data2), .id="Source")
# Create long data frames for lines data
dat2 = data.frame(Tempx, a1, a2)
dat3 = data.frame(Tempx, a1low, a1high, a2low, a2high)
dat2long = dat2 %>% gather(Source, value, -Tempx)
dat3long = dat3 %>% gather(Source, value, -Tempx)
ggplot() +
geom_point(data=dat1, aes(Temp, var1, shape=Source, colour=Source)) +
geom_line(data=dat2long, aes(x=Tempx, y=value, colour=Source)) +
geom_line(data=dat3long, aes(x=Tempx, y=value, group=Source), linetype=2,
colour=rep(c("red","blue"), each=2*length(Tempx))) +
scale_colour_manual(values=c("red","blue")) +
theme_bw()
If your goal is to plot confidence intervals, you can do this:
ggplot(data=dat1, aes(Temp, var1, shape=Source, fill=Source, colour=Source)) +
geom_point() +
geom_smooth(method='lm') +
theme_bw()
This is a bit messy but I think it does what you want it to. Sorry for the kooky colours, I just pulled some out from colorbrewer.
library(ggplot2)
Tempx<-c(20, 22, 24, 26, 28, 30, 32, 34)
Tempx<-rep(Tempx,2)
Temp<-rnorm(1000,28,2)
var1<-rnorm(1000,25,5)
var2<-rnorm(1000,12,5)
a1<-1.05*Tempx
a2<-0.5*Tempx
a1low<-0.95*Tempx
a1high<-1.15*Tempx
a2low<-0.4*Tempx
a2high<-0.6*Tempx
dat1 <- data.frame(Temp, var1, var2)
dat2 <- data.frame(Tempx, a1, a2, a1low, a1high, a2low, a2high)
plot2 <- ggplot() +
geom_point(data=dat1,aes(x=Temp,y=var1, colour="var1", size=1.5)) +
geom_point(data=dat1,aes(x=Temp, y=var2, colour="var2", size=1)) +
geom_line(data=dat2,aes(x=Tempx, y=a1, colour="a1"), size=1.75) +
geom_line(data=dat2,aes(x=Tempx,y=a2, colour="a2"), size=1.75) +
geom_line(data=dat2,aes(x=Tempx,y=a1low, colour="black",size=1.25, linetype="longdash")) +
geom_line(data=dat2,aes(x=Tempx,y=a1high, colour="black",size=1.25, linetype="longdash")) +
geom_line(data=dat2,aes(x=Tempx,y=a2low, colour="black",size=1.25, linetype="longdash")) +
geom_line(data=dat2,aes(x=Tempx,y=a2high, colour="black",size=1.25, linetype="longdash")) +
scale_color_manual("",
breaks = c("var1", "var2", "a1", "a2", "a1low", "a1high", "a2low", "a2high"),
values=c("#762a83", "#af8dc3","#e7d4e8","#f7f7f7","#d9f0d3","#7fbf7b","#1b7837", "#000000")) +
scale_size(guide = "none")+
guides(size="none", linetype="none")+ #removes linetype and sizes from legend
theme_bw()
:)
As suggested in the comment:
Let's start with cleaning up your data frames:
Temp<-rnorm(1000,28,2)
Data1<-data.frame(Temp,var = rnorm(1000,25,5), group = rep ('a', 1000)) ##adding groups
Data2<-data.frame(Temp,var = rnorm(1000,12,5), group = rep ('b', 1000))
data = rbind(Data1, Data2 )
now cleaning your ggplot call
A suggestion:
ggplot(data, aes(Temp, var, color = group )) +
geom_point(size=1.5, shape=1) +
geom_smooth(method = 'lm') #(maybe this model is what you want anyways)
You were actually very close with the scale_shape_manual and scale_colour_manual calls. The missing ingredient was putting the shapes for the two sets of points into a mapping for geom_point they way you did for colour in geom_line:
library(ggplot2)
plot2 <- ggplot(mapping=aes(x=Temp, y=var1)) +
geom_point(data=Data, mapping=aes(shape="high"), colour="grey50", size=1.5) +
geom_point(data=Data2, mapping=aes(shape="low")) +
scale_shape_manual(
values=c(1, 16),
guide=guide_legend(
title="Legend: Points",
override.aes=list(
colour=c("grey50", "black"),
size=c(1.5, 1)
)
)
) +
geom_line(mapping=aes(x=Tempx, y=a1, colour="high"), size=1.75) +
geom_line(mapping=aes(x=Tempx, y=a2, colour="low"), size=1.75) +
scale_colour_manual(values=c("grey50", "black"), guide=guide_legend(title="Legend: Lines")) +
geom_line(mapping=aes(x=Tempx, y=a1low), colour="grey50", size=1.25, linetype="longdash") +
geom_line(mapping=aes(x=Tempx, y=a1high), colour="grey50", size=1.25, linetype="longdash") +
geom_line(mapping=aes(x=Tempx, y=a2low), colour="black", size=1.25, linetype="longdash") +
geom_line(mapping=aes(x=Tempx, y=a2high), colour="black", size=1.25, linetype="longdash") +
theme_bw() +
theme(axis.line.x=element_line(colour="black"),
axis.line.y=element_line(colour="black"),
axis.text.x=element_text(margin=unit(c(0.4, 0.4, 0.4, 0.4), "cm")),
axis.text.y=element_text(margin=unit(c(0.4, 0.4, 0.4, 0.4), "cm")),
axis.title.x=element_text(margin=margin(t=-6)),
axis.title.y=element_text(margin=margin(t=-6)),
axis.text=element_text(size=13),
text=element_text(size=14),
plot.title=element_text(size=16, hjust=0),
axis.ticks.length=unit(-0.1,"cm"),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border=element_blank(),
panel.background=element_blank()) +
scale_x_continuous(name="Temperature", breaks=c(20, 22, 24, 26, 28, 30, 32, 34), expand=c(0, 0), limits=c(20, 34)) +
scale_y_continuous(name="Variable 1", expand=c(0, 0), limits=c(0, 40)) +
ggtitle("Title")
The code above gives a plot with two separate legends, one for points and one for lines:
Ok, lets take some sample data.
A <- sample(1:100, 25)
B <- sample(1:25, 25)
df.1 <- data.frame(A,B)
C <- sample(1:80, 15)
D <- sample(1:15, 15)
df.2 <- data.frame(C,D)
Then we plot the data using ggplot
library(ggplot2)
(plot2 <- ggplot(NULL) +
geom_point(data=df.1, aes(x=A, y=B),
color='black', cex=1, pch=16 ) +
geom_smooth(data=df.1, aes(x=A, y=B), method="lm", size=1,
se=FALSE, colour="black", linetype=2)+
geom_point(data=df.2, aes(x=C, y=D),
color='black', cex=1, pch=15 ) +
geom_smooth(data=df.2, aes(x=C, y=D), method="lm", size=1,
se=FALSE, colour="black", linetype=1)+
scale_y_continuous("Y scale") +
ggtitle("Plot") +
theme_bw()+
theme(plot.title = element_text(face="bold", size=20),
axis.title.x = element_text(vjust=-0.25),
axis.title.y = element_text(vjust=1),
axis.title = element_text(face="bold", size=15)
)
)
So we have created and modified the title, axis, etc.
But I want to create a legend which shows the linetype's from the geom_smooth() function of df.1 and df.2. It should be in the top right of the graph.
(so for df.1 we want a solid line and df.2 a dashed line)
The example here walks you through an example, but the data comes from within the same data set
Here you go:
#combine and create x and y (as mappings follow
#same pattern)
df.1$group <- "df.1"
df.1$x <- df.1$A
df.1$y <- df.1$B
df.2$group <- "df.2"
df.2$x <- df.2$C
df.2$y <- df.2$D
library(plyr) #for rbind.fill
df.all <- rbind.fill(df.1,df.2)
plot3 <- ggplot(df.all, aes(x=x,y=y,group=group)) +
geom_point(color='black', cex=1, pch=16 ) +
geom_smooth(aes(linetype=group),method="lm", size=1,
se=FALSE, colour="black") +
scale_y_continuous("Y scale") +
ggtitle("Plot") +
theme_bw()+
theme(plot.title = element_text(face="bold", size=20),
axis.title.x = element_text(vjust=-0.25),
axis.title.y = element_text(vjust=1),
axis.title = element_text(face="bold", size=15)
) +
#add custom linetypes (not necessary now, as default mapping to 1 and 2)
plot3 + scale_linetype_manual(values=c("df.1"=1,"df.2"=2))
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: