ggplot2, introduce breaks on a x log scale - r

I have a plot like this:
p<-ggplot() +
geom_line(data= myData, aes(x = myData$x , y = myData$y)) +
scale_x_log10()+
scale_y_log10()
My x value is seq(9880000, 12220000, 10000)
There is only one break on the x-axis of the plot, what should I do if to get at least 3 breaks on the plot x-axis?

Here is fully reproducible example of the original poster's problem where a log-scaled plot only displays one break value on the x-axis. I demonstrate three possible solutions below.
library(ggplot2)
# Create a reproducible example data.frame using R functions.
x = seq(9880000, 12220000, 10000)
# Use set.seed() so that anyone who runs this code
# will get the same sequence of 'random' values.
set.seed(31415)
y = cumsum(runif(n=length(x), min=-1e5, max=1e5)) + 1e6
dat = data.frame(x=x, y=y)
# Original poster's plot.
p1 = ggplot(data=dat, aes(x=x, y=y)) +
geom_line() +
scale_x_log10() +
scale_y_log10() +
labs(title="1. Plot has only one x-axis break.")
# Add extra x-axis breaks manually.
x_breaks = c(10^7.0, 10^7.04, 10^7.08)
p2 = ggplot(data=dat, aes(x=x, y=y)) +
geom_line() +
scale_x_log10(breaks=x_breaks) +
scale_y_log10() +
labs(title="2. Add some x-axis breaks manually.")
# Add extra x-axis breaks in semi-automated manner.
x_breaks = 10^pretty(log10(x))
x_labels = formatC(x_breaks, format = "e", digits = 2)
p3 = ggplot(data=dat, aes(x=x, y=y)) +
geom_line() +
scale_x_log10(breaks=x_breaks, labels=x_labels) +
scale_y_log10() +
labs(title="3. Create x-axis breaks with R functions.")
# Skip the log10 scale because the x-values don't span multiple orders of magnitude.
p4 = ggplot(data=dat, aes(x=x, y=y)) +
geom_line() +
scale_y_log10() +
labs(title="4. Check appearance without log10 scale for x-axis.")
library(gridExtra)
ggsave("example.png", plot=arrangeGrob(p1, p2, p3, p4, nrow=2),
width=10, height=5, dpi=150)

I add: scale_x_log10(breaks=seq(9880000, 12220000, 1000000)).
This is my reproducible example:
library(random)
library(ggplot2)
z <- randomStrings(n=235, len=5, digits=TRUE, upperalpha=TRUE, loweralpha=TRUE, unique=TRUE, check=TRUE)
x <- seq(9880000, 12220000, 10000)
y <- randomNumbers(n=235, min=9880000, max=12220000, col=1)
df <- data.frame(z, x, y)
head(df)
V1 x V1.1
1 378VO 9880000 11501626
2 AStRK 9890000 10929705
3 sotp4 9900000 11305700
4 AS4DR 9910000 11302110
5 7iFdk 9920000 11611918
6 HIS7z 9930000 11175074
p<-ggplot() + geom_line(data= df, aes(x = df$x , y = df$V1.1)) + scale_y_log10()
p + scale_x_log10(breaks=seq(9880000, 12220000, 1000000))
Hope it is useful...

Add this between your parenthesis: breaks=seq(specify, breaks, here)
For example, if you wanted a break at 0, 10, 100:
scale_x_log10((breaks=seq(0,10,100))

Related

R ggplot2 geom_ribbon: shade/coloring area bounded by two crossing lines on the sides when no line is below and no line is above

Problem: I want to color betwen the lines
Trick title, I know. I have three lines which look like this
I want to shade/color the area between the green and the red one. Basically the red and the green lines represent boundaries of some sort of confidence interval for the blue line. I can only colour half of it for now, and that's because the blue is below the green before intersection, and the green is below the blue after intersection. However, I cannot color the other half because the blue and the red one are divergent.
Is there a simple way to color between two almost vertical lines like this?
I tried geom_ribbon, but didn't get anywhere. Here is my current code, coloring only half.
Minimal Working Example
library(ggplot2)
# Lines to plot
blue_line <- function(x) 28.6*x - 51
red_line <- function(x) -16*x + 28
green_line <- function(x) 5.5*x-10
# Data to plot
x <- seq(from=1, to=4, length.out=200)
df <- data.frame(x=x, yblue=blue_line(x), yred=red_line(x), ygreen=green_line(x))
ggplot(data=df, aes(x=x)) +
geom_ribbon(aes(x=x, ymin=ygreen, ymax=yblue), fill="grey80") +
geom_line(aes(x=x, y=yblue), color="blue") +
geom_line(aes(x=x, y=yred), color="red") +
geom_line(aes(x=x, y=ygreen), color="green") +
coord_cartesian(xlim=c(-3.5, 8), ylim=c(-4, 12))
which produces
Second Minimal Working Example
This code works, however notice how tedious, repetitive and just plain stupid it is. Hopefully there's a much better way.
library(ggplot2)
# Lines to plot
blue_line <- function(x) 28.6*x - 51
red_line <- function(x) -16*x + 28
green_line <- function(x) 5.5*x-10
# Data to plot
x <- seq(from=0.9, to=4.2, length.out=200)
# Below
x_below <- seq(from=0.9, to=2.06, length.out=200)
y_below <- rep(-5, 200)
y_below_above <- c(green_line(x_below[x_below <= 1.76744]), red_line(x_below[x_below > 1.76744]))
# Above
x_above <- seq(from=-2.5625, to=4.181818, length.out=200)
y_above <- rep(13, 200)
y_above_below <- c(red_line(x_above[x_above<=1.76744]), green_line(x_above[x_above>1.76744]))
df <- data.frame(x=x, yblue=blue_line(x), yred=red_line(x), ygreen=green_line(x),
ybelow=y_below, xbelow=x_below, y_below_above=y_below_above,
xabove=x_above, yabove=y_above, y_above_below=y_above_below)
ggplot(data=df, aes(x=x)) +
geom_ribbon(aes(x=xbelow, ymin=ybelow, ymax=y_below_above), fill="grey80") +
geom_ribbon(aes(x=xabove, ymin=y_above_below, ymax=yabove), fill="grey80") +
geom_line(aes(x=x, y=yblue), color="blue") +
geom_line(aes(x=x, y=yred), color="red") +
geom_line(aes(x=x, y=ygreen), color="green") +
coord_cartesian(xlim=c(-3.5, 8), ylim=c(-4, 12))
producing the desired
Besides your example with data manipulation, I am not aware of how to fill using geom_ribbon from xmin to xmax without coord_flip as mentioned here.
However you can use geom_polygon to create a filled region between two lines as follows:
poly_df <- rbind(setNames(df[, c(1,3)],c('x','y')),
setNames(df[, c(1,4)],c('x','y')))
ggplot(data=df, aes(x=x)) +
geom_line(aes(y=yblue), color="blue") +
geom_line(aes(y=yred), color="red") +
geom_line(aes(y=ygreen), color="green") +
coord_cartesian(xlim=c(-3.5, 8), ylim=c(-4, 12)) +
geom_polygon(data = poly_df, aes(x = x,y = y), fill = "lightblue", alpha = 0.25)

Change axis breaks without defining sequence - ggplot

Is there any way to set the break step size in ggplot without defining a sequence. For example:
x <- 1:10
y <- 1:10
df <- data.frame(x, y)
# Plot with auto scale
ggplot(df, aes(x,y)) + geom_point()
# Plot with breaks defined by sequence
ggplot(df, aes(x,y)) + geom_point() +
scale_y_continuous(breaks = seq(0,10,1))
# Plot with automatic sequence for breaks
ggplot(df, aes(x,y)) + geom_point() +
scale_y_continuous(breaks = seq(min(df$y),max(df$y),1))
# Does this exist?
ggplot(df, aes(x,y)) + geom_point() +
scale_y_continuous(break_step = 1)
You may say I am being lazy but there have been a few occasions where I have had to change the min and max limits of my seq due to the addition of error bars. So I just want to say...use a break size of x, with automatic scale limits.
You can define your own function to pass to the breaks argument. An example that would work in your case would be
f <- function(y) seq(floor(min(y)), ceiling(max(y)))
Then
ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(breaks = f)
gives
You could modify this to pass the step of the breaks, e.g.
f <- function(k) {
step <- k
function(y) seq(floor(min(y)), ceiling(max(y)), by = step)
}
then
ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(breaks = f(2))
would create a y-axis with ticks at 2, 4, .., 10, etc.
You can take this even further by writing your own scale function
my_scale <- function(step = 1, ...) scale_y_continuous(breaks = f(step), ...)
and just call it like
ggplot(df, aes(x,y)) + geom_point() + my_scale()
> # Does this exist?
> ggplot(df, aes(x,y)) + geom_point() +
> scale_y_continuous(break_step = 1)
If you're looking for an off-the-shelf solution, then you can use the scales::breaks_width() function like so:
scale_y_continuous(breaks = scales::breaks_width(1))
The scales package also includes handy functions to control breaks easily in "special" scales such as date-time, e.g. scale_x_datetime(breaks='6 hours').

ggplot can not group bars

Who can tell me why ggplot can't give me grouped bars?
ggplot(df, aes(x = factor(labels), y = srednia, dodge=factor(group))) +
labs(title = gen, size=3)+ ylab("Fold change")+ xlab("Linnia komórkowa") +
geom_bar(aes(fill=factor(group)),stat="identity",position ="dodge") +
geom_errorbar(aes(ymin=minus, ymax=plus))
Grouped bars I means something like this (paint art):
Thank you in advance!
I guess you can achieve this by changing the scale for the x axis. Here's a reproducible example and a possible solution.
# packages
require(plyr)
require(ggplot2)
# generate data
set.seed(123)
df <- data.frame(labels=LETTERS[1:6],
group=rep(1:3, each=2),
srednia=runif(6))
# limits for x axis
mylims <- head(unlist(dlply(df, .(group), function(x) c(levels(factor(x$labels)), "space"))), -1)
# additional space between groups
ggplot(df, aes(x = factor(labels), y = srednia, dodge=factor(group))) +
geom_bar(aes(fill=factor(group)),stat="identity") +
scale_x_discrete(limits=mylims, breaks=levels(factor(df$labels)))
# removing space within group
ggplot(df, aes(x = factor(labels), y = srednia, dodge=factor(group))) +
geom_bar(aes(fill=factor(group)),stat="identity", width=1) +
scale_x_discrete(limits=mylims, breaks=levels(factor(df$labels)))

Grouping labels when x is a factor variable in ggplot2

I'm trying to replace the x-axis labels "A0" and "A1" by one "A" which can be placed in the middle of "A0" and "A1". It would be better if there is a method which works like the following question:
grouping of axis labels ggplot2
By that, I mean to redraw the x-axis only for each group, and leave a blank between groups.
Here is the code I'm working on:
y = 1*round(runif(20)*10,1)
x1 = c("A","B")
x2 = c(0,1)
x = expand.grid(x1,x2)
xy = cbind(x,y)
xy$z = paste(xy$Var1,xy$Var2,sep="")
p <- ggplot(xy, aes(x=factor(z), y=y,fill=factor(Var2)))
p + geom_boxplot() + geom_jitter(position=position_jitter(width=.2)) + theme_bw() + xlab("X") + ylab("Y") + scale_fill_discrete(name="Var2",breaks=c(0, 1),labels=c("T", "C"))
Try this. No need for the variable z, just use position="dodge":
p <- ggplot(xy, aes(x=factor(Var1), y=y,fill=factor(Var2)))
p + geom_boxplot(position="dodge") + geom_jitter(position=position_jitter(width=.2)) + theme_bw() + xlab("X") + ylab("Y") + scale_fill_discrete(name="Var2",breaks=c(0, 1),labels=c("T", "C"))

Plotting two variables using ggplot2 - same x axis

I have two graphs with the same x axis - the range of x is 0-5 in both of them.
I would like to combine both of them to one graph and I didn't find a previous example.
Here is what I got:
c <- ggplot(survey, aes(often_post,often_privacy)) + stat_smooth(method="loess")
c <- ggplot(survey, aes(frequent_read,often_privacy)) + stat_smooth(method="loess")
How can I combine them?
The y axis is "often privacy" and in each graph the x axis is "often post" or "frequent read".
I thought I can combine them easily (somehow) because the range is 0-5 in both of them.
Many thanks!
Example code for Ben's solution.
#Sample data
survey <- data.frame(
often_post = runif(10, 0, 5),
frequent_read = 5 * rbeta(10, 1, 1),
often_privacy = sample(10, replace = TRUE)
)
#Reshape the data frame
survey2 <- melt(survey, measure.vars = c("often_post", "frequent_read"))
#Plot using colour as an aesthetic to distinguish lines
(p <- ggplot(survey2, aes(value, often_privacy, colour = variable)) +
geom_point() +
geom_smooth()
)
You can use + to combine other plots on the same ggplot object. For example, to plot points and smoothed lines for both pairs of columns:
ggplot(survey, aes(often_post,often_privacy)) +
geom_point() +
geom_smooth() +
geom_point(aes(frequent_read,often_privacy)) +
geom_smooth(aes(frequent_read,often_privacy))
Try this:
df <- data.frame(x=x_var, y=y1_var, type='y1')
df <- rbind(df, data.frame(x=x_var, y=y2_var, type='y2'))
ggplot(df, aes(x, y, group=type, col=type)) + geom_line()

Resources