I would like to set the x and y axis in the following plot to have the same scale distance (i.e. 0.1 on the x axis is the same length as 0.1 on the y axis). Any advice? Thanks.
df <-data.frame(x = c(0,0.2,0.5), y = c(0.6,0.7,0.9))
p <-ggplot(df, aes(x, y, ymin=0, ymax=1, xmin=0, xmax=1))
p <- p + geom_point(alpha=2/10, shape=21, fill="blue", colour="black", size=5)
grid.arrange(p, p,ncol=1)
p
You need to use coord_equal()
df <-data.frame(x = c(0,0.2,0.5), y = c(0.6,0.7,0.9))
p <-ggplot(df, aes(x, y, ymin=0, ymax=1, xmin=0, xmax=1))
p <- p + geom_point(alpha=2/10, shape=21, fill="blue", colour="black", size=5)
p + coord_equal()
You need to set your width and height of your graphic device with height = 2*width
library('ggplot2')
library('gridExtra')
df <-data.frame(x = c(0,0.2,0.5), y = c(0.6,0.7,0.9))
p <-ggplot(df, aes(x, y, ymin=0, ymax=1, xmin=0, xmax=1))
p <- p + geom_point(alpha=2/10, shape=21, fill="blue", colour="black", size=5)
w <- 550
png("test.png", width=w, height=2*w, units="px")
grid.arrange(p, p,ncol=1)
dev.off()
Related
library(ggplot2)
f=data.frame(x=exp(rnorm(100)), y=exp(rnorm(100, sd=2)))
ggplot(f, aes(x=x, y=y)) + geom_point() + coord_fixed() + scale_x_log10() + scale_y_log10()
The ranges of x and y are not the same. How can I make the ranges be the same for x and y while showing all the points?
You can set limits in both of your log scales like this:
library(ggplot2)
f=data.frame(x=exp(rnorm(100)), y=exp(rnorm(100, sd=2)))
ggplot(f, aes(x=x, y=y)) +
geom_point() + coord_fixed() +
scale_x_log10(limits = c(0.1, 10)) +
scale_y_log10(limits = c(0.1, 10))
Created on 2023-01-20 with reprex v2.0.2
I want to have a line that crosses the chart at 45 degrees no matter what the x and y scale is. In this example the intercept for the abline shoud be around x=-3 and y=-0.5.
Below a few line of code:
x <- seq(1,10,1)
y <- sample(1:100, 10, replace=T)
df <- data.frame(x,y)
ggplot(df, aes(x=x, y=y)) +
geom_point() +
geom_abline(slope = 45) +
scale_y_continuous(breaks = scales::pretty_breaks(n = 10), limits = c(-10,10)) +
scale_x_continuous(breaks = scales::pretty_breaks(n = 10), limits = c(-2,10))
You would just add
ggplot2::annotation_custom(grid::linesGrob())
to your plot.
So you can do this:
x <- rnorm(100)
y <- rnorm(100)
df <- data.frame(x,y)
ggplot(df, aes(x=x, y=y)) +
geom_point() +
ggplot2::annotation_custom(grid::linesGrob())
or this
ggplot(df, aes(x=x)) +
geom_histogram() +
ggplot2::annotation_custom(grid::linesGrob())
If you want to change the look of the line you need to change the grob:
ggplot(df, aes(x=x, y=y)) +
geom_point() +
ggplot2::annotation_custom(grid::linesGrob(gp = grid::gpar(col = "red", lty = 2)))
How can I draw several lines between two facets?
I attempted this by plotting points at the min value of the top graph but they are not between the two facets. See picture below.
This is my code so far:
t <- seq(1:1000)
y1 <- rexp(1000)
y2 <- cumsum(y1)
z <- rep(NA, length(t))
z[100:200] <- 1
df <- data.frame(t=t, values=c(y2,y1), type=rep(c("Bytes","Changes"), each=1000))
points <- data.frame(x=c(10:200,300:350), y=min(y2), type=rep("Bytes",242))
vline.data <- data.frame(type = c("Bytes","Bytes","Changes","Changes"), vl=c(1,5,20,5))
g <- ggplot(data=df, aes(x=t, y=values)) +
geom_line(colour=I("black")) +
facet_grid(type ~ ., scales="free") +
scale_y_continuous(trans="log10") +
ylab("Log values") +
theme(axis.text.x = element_text(angle = 90, hjust = 1), panel.margin = unit(0, "lines"))+
geom_point(data=points, aes(x = x, y = y), colour="green")
g
In order to achieve that, you have to set the margins inside the plot to zero. You can do that with expand=c(0,0). The changes I made to your code:
When you use scale_y_continuous, you can define the axis label inside that part and you don't need a seperarate ylab.
Changed colour=I("black") to colour="black" inside geom_line.
Added expand=c(0,0) to scale_x_continuous and scale_y_continuous.
The complete code:
ggplot(data=df, aes(x=t, y=values)) +
geom_line(colour="black") +
geom_point(data=points, aes(x = x, y = y), colour="green") +
facet_grid(type ~ ., scales="free") +
scale_x_continuous("t", expand=c(0,0)) +
scale_y_continuous("Log values", trans="log10", expand=c(0,0)) +
theme(axis.text.x=element_text(angle=90, vjust=0.5), panel.margin=unit(0, "lines"))
which gives:
Adding lines can also be done with geom_segment. Normally the lines (segments) will appear in both facets. If you want them to appear between the two facets, you will have to restrict that in data parameter:
ggplot(data=df, aes(x=t, y=values)) +
geom_line(colour="black") +
geom_segment(data=df[df$type=="Bytes",], aes(x=10, y=0, xend=200, yend=0), colour="green", size=2) +
geom_segment(data=df[df$type=="Bytes",], aes(x=300, y=0, xend=350, yend=0), colour="green", size=1) +
facet_grid(type ~ ., scales="free") +
scale_x_continuous("t", expand=c(0,0)) +
scale_y_continuous("Log values", trans="log10", expand=c(0,0)) +
theme(axis.text.x=element_text(angle=90, vjust=0.5), panel.margin=unit(0, "lines"))
which gives:
I first plot histogram for a group of simulated data and fill the bars with one colour. Then I add the line of the density function from which the data was simulated from and make the line with a different colour. Now I want use legends to show one colour (the fill colour of the histogram) is for samples whereas the other (the colour of the line) is for theoretical density. How can I achieve this?
The code is as follows
require(ggplot2)
df <- data.frame(x=rnorm(10^4))
p <- ggplot(df, aes(x=x)) + geom_histogram(aes(y=..density..), fill='steelblue', colour='black', alpha=0.8, width=0.2)
x <- seq(-4, 4, 0.01)
df <- data.frame(x=x, y=dnorm(x))
p <- p + geom_line(data=df, aes(x=x, y=y), colour='red', size=1.5)
p
You can do this by adding a new column to each of your data frames to create fill and colour aesthetics to go into the legend. In each case, there's only one category, but putting them inside the aes() gives you the legends you want:
require(ggplot2)
df <- data.frame(x=rnorm(10^4), fill=rep("Sample", 10^4))
p <- ggplot(df, aes(x=x)) + geom_histogram(aes(y=..density.., fill=fill),
colour='black', alpha=0.8, width=0.2) +
scale_fill_manual(values="steelblue") + labs(fill="")
x <- seq(-4, 4, 0.01)
df <- data.frame(x=x, y=dnorm(x), colour=rep("Theoretical Density",length(x)))
p <- p + geom_line(data=df, aes(x=x, y=y, colour=line), size=1.5) +
scale_colour_manual(values="red") + labs(colour="")
Without changing your data at all, you can specify literal aes() values that you can define later via manual scales.
df <- data.frame(x=rnorm(10^4))
p <- ggplot(df, aes(x=x)) + geom_histogram(aes(y=..density.., fill="samples"),
alpha=0.8, colour="black", width=0.2)
p <- p+scale_fill_manual("",breaks="samples", values="steelblue")
x <- seq(-4, 4, 0.01)
df <- data.frame(x=x, y=dnorm(x))
p <- p + geom_line(data=df, aes(x=x, y=y, colour="theory"), size=1.5)
p <- p+scale_color_manual("",breaks="theory", values="red")
How can I avoid the grey shading of the plot area that occurs when plotting the following data?
df <-data.frame(x = c(0,0.2,0.5), y = c(0.6,0.7,0.9))
p <-ggplot(df, aes(x, y, ymin=0, ymax=1, xmin=0, xmax=1))
p <- p + geom_point(alpha=2/10, shape=21,
fill="blue", colour="black", size=5)
p
So fine up until this point but then adding a line equation using geom_smooth causes part of the background to become grey.
p <- p + geom_smooth(method="lm", se=FALSE, formula=y~x, colour="black")
p
Any suggestions on how to avoid this? Thanks.
Add fill=NA to your geom_smooth call:
p + geom_smooth(method="lm", se=FALSE, formula=y~x,colour="black",fill=NA)