My problem is that with ggplot, when the x-axis contains dates a little too muuch excess space is given in the plot on the left side
Is there any possibility for the red space to be removed? so that the whole curve and x-axis is shifted to the left.
library(tidyverse)
set.seed(138)
mydat <- (as.Date("2021-04-01")+0:99) %>% as.data.frame()
y <- rnorm(100)
mydat$val <- y
names(mydat) <- c("Time", "Value")
ggplot(mydat, aes(x=as.Date(Time)))+geom_line(aes(y=Value))+
scale_x_date(breaks = c(seq(as.Date('2021-04-01'), as.Date('2021-08-01'), by="2 week")) , date_labels = "%d-%b-%y ", limits=c(as.Date('2021-04-01'), as.Date('2021-07-09')))+theme(legend.position="bottom")+
theme(
axis.text.x = element_text(angle=45,size=10, vjust = 0.5))
Add the expand =c(0,0) parameter in the scale_x_date
Related
I would like to remove every n-th x-axis tick labels from a geom_boxplot (ggplot).
For example take this dummy dataframe:
Lat <- c(rep(50.70,3), rep(51.82,3), rep(52.78,3), rep(56.51,3))
y <- c(seq(1,2, by=0.5), seq(1,3, by=1), seq(2,6,by=2), seq(1,5,by=2))
df <- as.data.frame(cbind(Lat, y))
I can make a ggplot boxplot like so:
box_plot <- ggplot(df, aes(x=as.factor(Lat), y=y))+
geom_boxplot()+
labs(x="Latitude")+
scale_y_continuous(breaks = pretty_breaks(n=6)) +
theme_classic()
box_plot
However I would like to remove the labels from the middle two boxes.
I know I can achieve this by changing the labels to simply be blank (as below).
However, my real dataframe has many more than 4 ticks so this would be time consuming never mind more likely for human error!
box_plot2 <- ggplot(df, aes(x=as.factor(Lat), y=y))+
geom_boxplot()+
labs(x="Latitude")+
scale_y_continuous(breaks = pretty_breaks(n=6)) +
scale_x_discrete(labels=c("50.70", " ", " ", "56.51"))+
theme_classic()
box_plot2
Is there a way to produce the above plot without having to manually set the labels?
For example label every n-th tick on the x axis?
Thanks in advance!
This can be achieved like. As an example I just plot "every" third tick. Basic idea is to add an index for the factor levels. This index can then be used to specify the breaks or ticks one wants to plot. Try this:
Lat <- c(rep(50.70,3), rep(51.82,3), rep(52.78,3), rep(56.51,3))
y <- c(seq(1,2, by=0.5), seq(1,3, by=1), seq(2,6,by=2), seq(1,5,by=2))
df <- as.data.frame(cbind(Lat, y))
library(ggplot2)
library(scales)
library(dplyr)
df <- df %>%
mutate(Lat1 = as.factor(Lat),
Lat1_index = as.integer(Lat1))
# Which ticks should be shown on x-axis
breaks <- df %>%
# e.g. plot only every third tick
mutate(ticks_to_plot = Lat1_index %% 3 == 0) %>%
filter(ticks_to_plot) %>%
pull(Lat1)
box_plot2 <- ggplot(df, aes(x=Lat1, y=y))+
geom_boxplot()+
labs(x="Latitude")+
scale_y_continuous(breaks = pretty_breaks(n=6)) +
scale_x_discrete(breaks = breaks)+
theme_classic()
box_plot2
Created on 2020-03-30 by the reprex package (v0.3.0)
ggplot lets me control the position of my x-axis labels and breaks in x-axis but when I pass the ggplot object to ggplotly function, the resulting plotly object loses the formatting.
library(plotly)
df <- data.frame(
Date = seq(as.Date("2017-01-01"), as.Date("2020-01-01"), by = 30),
Value = rnorm(37)
)
p1 = ggplot(df) + geom_point(aes(x=Date, y = Value)) +
scale_x_date(position = "top", date_breaks = "1 year", date_minor_breaks =
"3 months")
ggplotly(p1)
With the code mention above the x-axis values are still plotted at the bottom in ggplotly plot and also the break lines every 3 months are not shown.
You could try this:
f <- list(
side = "top"
)
ggplotly(p1) %>% layout(xaxis = f)
I want the x-axis in the following graph to start at 06:00 and end at 22:00, with breaks at every 4 hours. I can't figure out the following, however.
a) How to make the x-axis start at 06:00 without any empty space before 06:00.
b) How to make the x-axis end at 22:00 without any empty space after 22:00. Right now it doesn't even show 22:00
c) How to have breaks at every 4 hours.
d) How to assign a label to the y-axis (currently it's simply X4, the column name).
I've tried several things, but without success. Some example data:
range <- seq(as.POSIXct("2015/4/18 06:00"),as.POSIXct("2015/4/18 22:00"),"mins")
df <- data.frame(matrix(nrow=length(range),ncol=4))
df[,1] <- c(1:length(range))
df[,2] <- 2*c(1:length(range))
df[,3] <- 3*c(1:length(range))
df[,4] <- range
Reshape:
library(reshape2)
df2 <- melt(df,id="X4")
Graph:
library(ggplot2)
ggplot(data=df2,aes(x=X4,y=value,color=variable)) + geom_line()+
scale_y_continuous(expand=c(0,0)) +
coord_cartesian(xlim=c(as.POSIXct("2015/4/18 06:00:00"),as.POSIXct("2015/4/18 22:00:00")))
Which makes the graph look like this:
Any ideas?
Here is some code that should help you. This can easily be done using scale_x_datetime.
## desired start and end points
st <- as.POSIXct("2015/4/18 06:00:00")
nd <- as.POSIXct("2015/4/18 22:00:00")
## display data for given time range
ggplot(data = df2, aes(x = X4, y = value, color = variable)) +
geom_line() +
scale_y_continuous("Some name", expand = c(0, 0)) +
scale_x_datetime("Some name", expand = c(0, 0), limits = c(st, nd),
breaks = seq(st, nd, "4 hours"),
labels = strftime(seq(st, nd, "4 hours"), "%H:%S"))
I want to check the boxplots for each year for variable a in the df data.frame. I used the code below to create the data.frame
set.seed(123)
date <- as.Date(seq(as.Date("1990-01-01"), as.Date("2015-12-31"), by = 1), format="%Y-%m-%d")
a <- runif(9496, 3000, 120000)
df <- data.frame(date, a)
df[c(1:151,9313:9496), 2]<-NA
and using this code
library(ggplot2)
ggplot(df, aes(x=date, y=a, group=years(date)))+
geom_boxplot()+
scale_x_date(breaks = date_breaks("1 year"),
labels = date_format("%Y"))
I got this figure
The years on x axis are shown before and after the boxplot. How can I align the ticks of x axis and axis.text with the boxplots?
You can set the date breaks to be in the middle of each year:
scale_x_date(breaks = seq(as.Date("1990-06-30"), as.Date("2015-06-30"), by="1 year"),
labels = date_format("%Y"))
This question already has answers here:
Plot with reversed y-axis and x-axis on top in ggplot2
(3 answers)
Closed 3 years ago.
I want to make a figure which have reversed y-axis and x-axis at y=0.
y axis was reversed with scale_y_reverse, but x-axis stayed at the bottom.
p <- ggplot(df, aes(x= conc, y=depth, group=factor(stn), color=factor(stn)))+
geom_point(shape=1)+
geom_path(alpha=0.5)+
scale_y_reverse(limits=(c(20,0)), expand=c(0,0))+
scale_x_continuous(expand=c(0,0))
I tried the code from this post like in below, but didn't work.
p +
scale_x_continuous(guide = guide_axis(position = "top")) +
scale_y_continuous(guide = guide_axis(position = "right"))
I don't need to have two x-axis, simply just move from bottom to the top.
This is still not possible in ggplot2, but it is possible in ggvis, which combines the ggplot2 grammer with dplyr pipelines. Just use the add_axis function to put the axis at the top.
# sample data
N <- 20
df <- data.frame(conc = seq(0, N),
depth = runif(N+1, 0, 20),
stn = rep(1:4, length=N+1))
# ggplot version
require(ggplot2)
p <- ggplot(df, aes(x= conc, y=depth, group=factor(stn), color=factor(stn)))+
geom_point(shape=1)+
geom_path(alpha=0.5)+
scale_y_reverse(limits=(c(20,0)), expand=c(0,0))+
scale_x_continuous(expand=c(0,0))
p
# ggvis version
require(ggvis)
df %>% transform(stn = factor(stn)) %>%
ggvis(x = ~conc, y = ~depth, stroke = ~stn) %>%
layer_points(shape := "circle", fill := "white") %>%
layer_lines(opacity := 0.5) %>%
scale_numeric("y", reverse=TRUE, domain=c(0,20), expand=c(0,0)) %>%
scale_numeric("x", expand=c(0,0)) %>%
add_axis("x", orient = "top")
You can also use:
library(cowplot)
ggdraw(switch_axis_position(p, axis = 'x'))