Tick division and labels causing confusion in ggplot2 [duplicate] - r

This question already has answers here:
How to display only integer values on an axis using ggplot2
(13 answers)
Closed 6 years ago.
When I plot the example below the x-axis labels are confusing since the ticks labeled 2008 and 2012 are 2008.5 and 20012.5. I fiddled and realized that the number format gets truncated because it's so long. What's a good way to floor the ticks and labels?
library(ggplot2)
foo <- data.frame(x=2005:2014,y=rnorm(10))
p1 <- ggplot(data = foo, aes(x = x, y = y))
p1 <- p1 + geom_point(size=4)
p1
EDIT: THis is indeed a duplicate question:
How to display only integer values on an axis using ggplot2
My apologies.

This two options could be an idea....
option 1:
foo <- data.frame(x=2005:2014,y=rnorm(10))
p1 <- ggplot(data = foo, aes(x = x, y = y))
p1 <- p1 + geom_point(size=4)
p1 <- p1 + scale_x_continuous(breaks = foo$x)
p1
option 2:
foo <- data.frame(x=2005:2014,y=rnorm(10))
p1 <- ggplot(data = foo, aes(x = x, y = y))
p1 <- p1 + geom_point(size=4)
p1 <- p1 + scale_x_continuous(breaks = foo$x[seq(1, length(foo$x), 2)])
p1

Related

how to only display decimals in ggplot axis tick labels? [duplicate]

This question already has answers here:
R - ggplot axis number format - remove leading zero
(2 answers)
Number formatting in labels in ggplot2?
(1 answer)
Closed 9 months ago.
How can I get ggplot to display only decimals in the axis tick labels?
Consider a toy dataframe like the following, for which all the variables are < 1 in absolute value:
set.seed(42)
x <- rnorm(n=500, sd = 0.1)
y <- 0.05 + 2*x + rnorm(n=500, sd = 0.05)
df <- data.frame(x,y)
summary(df)
library(ggplot2)
ggplot(data = df, aes(x = x, y = y)) +
geom_point()
Is it possible to have .xx instead of 0.xx on the axis?
You can pass a function to the labels argument of the scale_x_continuous(), in this case a lambda style function:
ggplot(data = df, aes(x = x, y = y)) +
geom_point() +
scale_x_continuous(labels=~sub("^(-?)0.", "\\1.", sprintf("%.1f", .x)))

How to draw a "U" shapped graph in R [duplicate]

This question already has answers here:
What type of graph is this? And can it be created using ggplot2?
(2 answers)
Closed 2 years ago.
I need to plot a "U-Shape" graph with ggplot, but i didn't get a nicer result with this code:
df <- data.frame(y = seq(0,8,1),
x = c(10,8,7,3,1,3,7,8,10))
ggplot(df, aes(y = y)) +
geom_line(aes(x = x))
can someone give me a help?
Something like this?
x = seq(-10,10,1)
df <- data.frame(x = x, y = x^2)
ggplot(df, aes(y = y, x = x)) +
geom_line()
It looks like geom_path is what you're looking for here, rather than geom_line. geom_path connects the values in the order they're in the data frame, while geom_line in the order along the x-axis.
df <- data.frame(y = seq(0,8,1),
x = c(10,8,7,3,1,3,7,8,10))
ggplot(df, aes(y = y)) +
geom_path(aes(x = x))
This is one possible solution.
library(ggplot2)
f <- function(x) 1 + x^2
x <- seq(-10, 10)
y <- f(x)
ggplot(data.frame(x, y), aes(x=x, y=y)) + geom_line()

How to make ggplot lines run to the edge?

I have data (depth over time) that I want to display with a line plot. For clarity, I want to zoom in on a section but still show the user that the data continues outside the bounds of the plot. So I want the lines to stop at the plot's edge, rather than at the last point. This is straightforward enough in base graphics but I can't make it work in ggplot. Here's an example with base:
d <- data.frame(x = 1:10, y = 1:10)
plot(d$x, d$y, xlim = c(2,9))
lines(d$x, d$y)
A similar approach with ggplot doesn't work; the lines stop at the last point. Example:
d <- data.frame(x = 1:10, y = 1:10)
ggplot(d, aes(x, y)) + geom_point() + geom_line() + xlim(2,9)
Is there a way to get lines to run to the plot's edge in ggplot? Thanks.
try this
d <- data.frame(x = 1:10, y = 1:10)
ggplot(d, aes(x, y)) + geom_point() + geom_line() + coord_cartesian(xlim = c(0,9))
if you want a straight line, abline would be easiest
d <- data.frame(x = 1:10, y = 1:10)
ggplot(d, aes(x, y)) + geom_point() + geom_abline() + xlim(2,9)

Align x axes of box plot and line plot using ggplot

Im trying to align the x-axes of a bar plot and line plot in one window frame using ggplot. Here is the fake data I'm trying to do it with.
library(ggplot2)
library(gridExtra)
m <- as.data.frame(matrix(0, ncol = 2, nrow = 27))
colnames(m) <- c("x", "y")
for( i in 1:nrow(m))
{
m$x[i] <- i
m$y[i] <- ((i*2) + 3)
}
My_plot <- (ggplot(data = m, aes(x = x, y = y)) + theme_bw())
Line_plot <- My_plot + geom_line()
Bar_plot <- My_plot + geom_bar(stat = "identity")
grid.arrange(Line_plot, Bar_plot)
Thank you for your help.
#eipi10 answers this particular case, but in general you also need to equalize the plot widths. If, for example, the y labels on one of the plots take up more space than on the other, even if you use the same axis on each plot, they will not line up when passed to grid.arrange:
axis <- scale_x_continuous(limits=range(m$x))
Line_plot <- ggplot(data = m, aes(x = x, y = y)) + theme_bw() + axis + geom_line()
m2 <- within(m, y <- y * 1e7)
Bar_plot <- ggplot(data = m2, aes(x = x, y = y)) + theme_bw() + axis + geom_bar(stat = "identity")
grid.arrange(Line_plot, Bar_plot)
In this case, you have to equalize the plot widths:
Line_plot <- ggplot_gtable(ggplot_build(Line_plot))
Bar_plot <- ggplot_gtable(ggplot_build(Bar_plot))
Bar_plot$widths <-Line_plot$widths
grid.arrange(Line_plot, Bar_plot)
The gridlines on the x axes will be aligned if you use scale_x_continuous to force ggplot to use limits you specify.
My_plot <- ggplot(data = m, aes(x = x, y = y)) + theme_bw() +
scale_x_continuous(limits=range(m$x))
Now, when you add the layers, the axes will share the common scaling.

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

Resources