How to use scale_x_date properly - r

I am a new user in R and I hope you can help me.
setwd("C:/Users/USER/Desktop/Jorge")
agua <- read_excel("agua.xlsx")
pbi <- read_excel("PBIagro.xlsx")
str(agua);
names(agua)[2] <- "Variación";
agua[,1] <- as.Date(agua$Trimestre)
lagpbi <- lag(pbi$PBIAgropecuario, k=1)
pbi[,3]<- lagpbi; pbi <- pbi[-c(1),];
names(pbi)[3] <- "PBIlag"
growth <- ((pbi$PBIAgropecuario-pbi$PBIlag)/pbi$PBIlag)*100
Anual_growth <- data.frame(growth); Anual_growth[,2] <- pbi$Año; names(Anual_growth)[2] <- "Año"
# Plot
Agro <- ggplot(Anual_growth, aes(x=Año, y=growth)) +
geom_line(color="steelblue") +
geom_point() +
geom_text(aes(label = round(Anual_growth$growth, 1)),
vjust = "inward", hjust = "inward", size=2.5, show.legend = FALSE) +
xlab("") +
theme_ipsum() +
theme(axis.text.x=element_text(angle=60, hjust=1)) +
ylim(-9.9,13.4) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.line.x = element_blank(), plot.margin = unit(c(1,1,0.5,1),"cm"),
axis.line.y = element_blank(), axis.text.x=element_text(face = "bold", size=8,
angle=1,hjust=0.95,vjust=0.2),
axis.text.y = element_blank(), axis.title.y=element_blank())+
scale_x_continuous("Año", labels = as.character(Anual_growth$Año), breaks = Anual_growth$Año)
print(Agro)
The problem is that it shows all the years, but I only want pair years (in X-axis) or years with step equal to 2.
I hope you can really help me.
Thank you.
Notice that the X-axis variable is a numeric string.

You can add something like
scale_x_date(date_breaks = "2 years", date_labels = "%Y") to your ggplot.
This is how it looks with my data, since you haven't posted yours. I am plotting a type date on x axis.
1.
ggplot(mydata) +
aes(x = date, y = number, color = somevar) +
geom_line()
ggplot(mydata) +
aes(x = date, y = number, color = somevar) +
geom_line() +
scale_x_date(date_breaks = "1 year", date_labels = "%Y")
3.
ggplot(mydata) +
aes(x = date, y = number, color = somevar) +
geom_line() +
scale_x_date(date_breaks = "2 years", date_labels = "%Y")

If you want pair years and because your x-axis variable is numeric, you can specify in scale_x_continous that breaks argument should take only even numbers.
Here how you can do it using this small example:
year = 1998:2020
value = rnorm(23,mean = 3)
df = data.frame(year,value)
library(ggplot2)
ggplot(df, aes(x = year, y = value))+
geom_point()+
geom_line()+
scale_x_continuous(breaks = year[year %%2 ==0])
Reciprocally, if you want odd years, you just have to specify scale_x_continuous(breaks = year[year %%2 != 0])
So, in your code, you should write:
scale_x_continuous(breaks = Anual_growth$Año[Anual_growth$Año %%2 ==0])
Does it answer your question ?

Related

In R, how can I extend a posixct timeseries axis beyond the end of the data

The issue I am trying to fix is that one of my temperature loggers stopped early. This causes the x axis to be a different length to the others (See image Axis). I want to extend the axis to be the same for plotting next to each other with an end date/time of 2022-06-06 00:00:00.
Any suggestions on how to adjust this? I've pasted the main 3 chunks of code used to transform data and plot below.
Thanks
SD1b <- SaxDeep1b
SD1b$datetime <- as.POSIXct(strptime(SD1b$datetime,format="%d/%m/%Y %H:%M",tz="Australia/Brisbane"))
head(SD1b)
SD1comb$datehour <- cut(as.POSIXct(SD1comb$datetime, format="%Y-%m-%d %H:%M:%S"),breaks="hour")
SD1hourlyT <- aggregate(temp ~ datehour,SD1comb,mean)
head(SD1hourlyT)
SD1hourlyT$datehour <- as.POSIXct(strptime(SD1hourlyT$datehour,format = "%Y-%m-%d %H",
tz="Australia/Brisbane"))
str(SD1hourlyT)
jpeg(file='SD1_temp.jpeg',width=19,height=10,res=1200,units="cm")
SD1temp <- ggplot(SD1hourlyT, aes(x = datehour, y = temp)) +
geom_line(colour="black") +
labs(x=element_blank(),y=expression("Temperature " ( degree*C)) ) +
scale_x_datetime(date_breaks="1 month",date_labels = "%b") + #see ?strptime for labs
scale_y_continuous(limits=c(23,33),breaks=c(23,25,27,29,31,33)) +
theme_linedraw() +
theme_minimal()+
theme(axis.text.x = element_text(colour="black",size=10),
axis.title.x = element_text(color = "black", size=12),
panel.grid.major = element_line(colour = "#d3d3d3"),
panel.grid.minor = element_blank(),
axis.text.y = element_text(colour="black",size=10),
axis.title.y = element_text(color = "black", size=12)) +
ggtitle("Saxon Deep 1")
You can use the limits argument within scale_x_datetime to expand the scale.
library(dplyr)
library(ggplot2)
library(scales)
library(lubridate)
# Example data
df <- data.frame(date = as.POSIXct(
c(
"2016-12-05-16.25.54.875000",
"2016-12-06-15.25.54.875000",
"2016-12-08-14.25.54.875000",
"2016-12-09-13.25.54.875000",
"2016-12-09-12.25.54.875000",
"2016-12-10-11.25.54.875000",
"2016-12-10-10.25.54.875000",
"2016-12-11-07.25.54.875000"
),
format = "%Y-%m-%d-%H.%M.%S"
) ,
y = 1:8)
Default axis limits
The minimum and maximum values of date are the default limits, even if there is no label or tick mark at the spot.
ggplot(df, aes(x = date, y = y)) +
geom_point() +
scale_x_datetime(labels = date_format("%D"),
date_breaks = "2 day")
Expanded axis limits
We can expand the axis limits even to values not observed in our data. Once again, you'll need to adjust labels and tick marks if you want to scale them the same as well.
ggplot(df, aes(x = date, y = y)) +
geom_point() +
scale_x_datetime(
labels = date_format("%D"),
date_breaks = "2 day",
limits = ymd_h("2016-12-05 00", "2016-12-20 23")
)

scale_x_date and how to make it equidistant?

How can please using ggplot2 package or whatever else package remove that "blank space " between months caused by absence of certain months on my x axis ? In other words to make the x axis looks equidistant and not having those "blank gapes".By the code is very normal ,it is about plotting certain values vs other column containing dates (not all the months are present in that date column ).
filtredplot1<-reactive({
req(res_mod())
dat<-res_mod()
dt<-dat[dat$M_Datum >= input$dateRange[1] & dat$M_Datum <= input$dateRange[2],]
dt[,5]<-as.Date(format(as.Date(dt[,5]), "%Y-%m-01"))
req(dt$M_Datum,dt$Yield)
dr<-data.frame("M_Datum"=dt$M_Datum,"Yield"=dt$Yield)
mydf=aggregate(Yield ~ M_Datum, dr, length)
req(mydf$M_Datum,mydf$Yield)
koka<-data.frame("M_Datum"=mydf$M_Datum,"Yiel"=mydf$Yield)
ggplot(koka, aes(x=factor(format(M_Datum, "%b %Y")), y=Yiel,group = 1)) +
geom_point(size=7,colour="#EF783D",shape=17) +
geom_line(color="#EF783D")+
scale_x_date(labels="%b %Y")
theme(axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1))+
theme(axis.text.y.left = element_text(color = "#EF783D"),
axis.title.y.left = element_text(color = "#EF783D"))+
ylab("Wafer Quantity")+
xlab("")
})
The code below does not create the data.frames dr and mydf, your data preparation code is too complicated. The following is much simpler and works.
Also, you have the typo Yiel for Yield twice in your code. The first when creating koka and the second in aes().
suppressPackageStartupMessages({
library(shiny)
library(ggplot2)
})
dt <- data.frame(
M_Datum=c("2018-02-05","2018-02-15","2018-02-10","2018-02-13","2017-02-05",
"2017-02-15","2017-02-10","2017-02-23","2020-02-25","2012-02-15",
"2020-02-10","2020-02-13"),
Yield=c(4,47,18,10,22,50,70,120,150,400,60,78)
)
dt$M_Datum <- as.Date(dt$M_Datum)
req(dt$M_Datum, dt$Yield)
koka <- aggregate(Yield ~ M_Datum, dt, length)
ggplot(koka, aes(x = M_Datum, y = Yield, group = 1)) +
geom_point(size = 7, colour = "#EF783D", shape = 17) +
geom_line(color = "#EF783D") +
scale_x_date(date_breaks = "1 month", date_labels = "%b %Y") +
xlab("") +
ylab("Wafer Quantity") +
theme(
axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
axis.text.y.left = element_text(color = "#EF783D"),
axis.title.y.left = element_text(color = "#EF783D")
)

Why are dates showing as numbers instead of as months in ggplot in R?

My x axis is showing number instead of months, how can I modify so it shows January, February and March?
data<- data.frame(Dates= rep(
seq(as.Date('2017-01-01'), as.Date('2017-03-03'), by = 'months')),
A=c(28.0,20.6,15.8),
B= c(0,12.7,6.5),
C= c(0,1.49,6.96),
Variable1= c(1,1,1))
trans_x <- function(x)round(coef(m1)[1] + coef(m1)[2]*x)
ggplot() +
geom_scatterpie(data = data, aes(x = Dates , y = Variable1, group = Dates, r=4), cols = c("A","B","C")) +
scale_y_log10() +
coord_fixed()+
theme_classic()+
theme(axis.text.y = element_blank())+
scale_fill_grey()
As #Mohanasundaram said right in the comments, you can use the scales package to format your date using date_format in the scale_x_date function like this:
library(tidyverse)
library(scatterpie)
library(scales)
ggplot() +
geom_scatterpie(data = data, aes(x = Dates , y = Variable1, group = Dates, r=4), cols = c("A","B","C")) +
scale_y_log10() +
coord_fixed()+
theme_classic()+
theme(axis.text.y = element_blank())+
scale_fill_grey() +
scale_x_date(labels = date_format("%Y-%B-%d"))
Output:

Displaying R plots in a 1x4 grid, using a shared y-axis

I am trying to display some graphs in a 1x4 grid, but I would like all the graphs to have the same x and y axes.
time maxhgs.sleep_LIPA maxhgs.sed_LIPA maxhgs.stand_LIPA maxhgs.MVPA_LIPA maxhgs.LIPA_MVPA
1 5 0.08289621 0.03241295 0.1129983 0.112998341 -0.01928050
2 10 0.16289049 0.06139545 0.2236818 -0.006728721 -0.04950022
3 15 0.24025861 0.08721203 0.3323473 -0.047756360 -0.08927656
4 20 0.31524160 0.11009218 0.4392581 -0.144261526 -0.13791276
5 25 0.38805152 0.13023596 0.5446498 -0.424789999 -0.19517306
6 30 0.41660977 0.13756729 0.5864293 -0.934884300 -0.26117695
This is the data I am working with.
library(ggplot2)
library(egg)
maxhgs.a <- ggplot(maxhgs.df, aes(time, maxhgs.sleep_LIPA)) + geom_point()+geom_line()
maxhgs.a <- maxhgs.a + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
scale_y_continuous(name = "Change in maxhgs", breaks = seq(0,1, by=0.1))+
ggtitle("Sleep to LIPA")
maxhgs.b <- ggplot(maxhgs.df, aes(time, maxhgs.sed_LIPA)) + geom_point()+geom_line()
maxhgs.b <- maxhgs.b + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
scale_y_continuous(name = "Change in maxhgs", breaks = seq(0,1, by=0.1))+
ggtitle("Sedentary to LIPA")
maxhgs.c <- ggplot(maxhgs.df, aes(time, maxhgs.stand_LIPA)) + geom_point()+geom_line()
maxhgs.c <- maxhgs.c + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
scale_y_continuous(name = "Change in maxhgs", breaks = seq(0,1, by=0.1))+
ggtitle("Standing to LIPA")
maxhgs.d <- ggplot(maxhgs.df, aes(time, maxhgs.MVPA_LIPA)) + geom_point()+geom_line()
maxhgs.d <- maxhgs.d + scale_x_continuous(name = "Time Reallocated", breaks = seq(5,30, by=5)) +
scale_y_continuous(name = "Change in maxhgs", breaks = seq(0.5,-1, by=-0.1))+
ggtitle("MVPA to LIPA")
ggarrange(maxhgs.a,
maxhgs.b +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank() ),
maxhgs.c +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank() ),
maxhgs.d +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank() ),
nrow = 1)
This is what I have attempted so far. This actually "works" in that all the graphs have the same y-axis, but the y-axis doesn't actually reflect what should be on the graphs. As you can see in the graph, the y-axis goes from 0.1 to 0.4, but the maxhgs.d graph should extend from 0.1 to -0.9.
Any advice or suggestions would be greatly appreciated!
You can make this much easier by reshaping your data and using faceting. That way, you only need to define a single plot. This requires you to pivot_longer and change the factor levels to the names you want for each facet, but once this is done, the plot itself is straightforward:
library(ggplot2)
library(dplyr)
# Define the label names for the facets first
labs <- c("LIPA to MVPA", "MVPA to LIPA", "Sedentary to LIPA",
"Sleep to LIPA", "Standing to LIPA")
gg <- maxhgs.df %>%
tidyr::pivot_longer(cols = -1) %>%
mutate(plot = factor(`levels<-`(factor(name), labs), labs[c(4, 3, 5, 2, 1)])) %>%
ggplot(aes(x = time, y = value)) +
geom_line() +
geom_point() +
scale_x_continuous(name = "Time Reallocated") +
scale_y_continuous(name = "Change in maxhgs") +
theme(strip.background = element_blank(),
strip.text = element_text(size = 13))
Now we can either choose to plot with fixed y axes:
gg + facet_grid(.~plot, scale = "fixed")
or with flexible y axes:
gg + facet_wrap(.~plot, scale = "free_y", ncol = 5)
Created on 2020-08-04 by the reprex package (v0.3.0)

Multiple plots in one figure in R

I have three plots and I want to show them in a figure like below
link
I made a few attempts but I was not successful. My codes are given below:
dat <- read.table(text="
dates PS.230 PS.286 PS.389
3.01.2018 20.75103 16.69312 -6.503637
15.01.2018 15.00284 16.03211 16.1058
8.02.2018 11.0789 7.438522 -2.970704
20.02.2018 15.10865 12.8969 3.935687
4.03.2018 24.74799 19.25148 9.186779
28.03.2018 -1.299456 7.028817 -8.126284
9.04.2018 4.778902 8.309322 -3.450085
21.04.2018 7.131915 9.484932 -4.326919
", header=T, stringsAsFactors=FALSE)
dat$dates <- as.Date(dat$dates, "%d.%m.%Y")
library(ggplot2)
library(tidyverse)
a <- ggplot(dat, aes(x=dates, y=PS.230)) +
geom_point() +
geom_line() +
geom_smooth(se = FALSE, method = lm, size = 0.15, color = "#da0018") + #cizgi eklemek icin
scale_x_date(date_breaks = "1 months",date_labels = "%Y-%m",
limits = as.Date.character(c("01/12/2017","31/12/2018"),
format = "%d/%m/%Y")) +
ylim(-20,40) +
ylab("[mm/year]") +
xlab("") +
theme_linedraw() #theme_light
a + theme(
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()
)
b <- ggplot(dat, aes(x=dates, y=PS.286)) +
geom_point() +
geom_line() +
geom_smooth(se = FALSE, method = lm, size = 0.15, color = "#da0018") + #cizgi eklemek icin
scale_x_date(date_breaks = "1 months",date_labels = "%Y-%m",
limits = as.Date.character(c("01/12/2017","31/12/2018"),
format = "%d/%m/%Y")) +
ylim(-20,40) +
ylab("[mm/year]") +
xlab("") +
theme_linedraw() #theme_light
b + theme(
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()
)
c <- ggplot(dat, aes(x=dates, y=PS.389)) +
geom_point() +
geom_line() +
geom_smooth(se = FALSE, method = lm, size = 0.15, color = "#da0018") + #cizgi eklemek icin
scale_x_date(date_breaks = "1 months",date_labels = "%Y-%m",
limits = as.Date.character(c("01/12/2017","31/12/2018"),
format = "%d/%m/%Y")) +
ylim(-20,40) +
ylab("[mm/year]") +
xlab("") +
theme_linedraw() #theme_light
c + theme(
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()
)
in the link I provided, much better graphics were drawn with fewer lines. my codes seem a little more complicated and frankly i couldn't get out. a, b and c plots in one image and only one date axes. How can I modify the codes to achieve sample result? Thank you.
Thank you for posting your data. As mentioned, the first step is to arrange your dataset so that it is in Tidy Data format. The information in dat$PS.230, dat$PS.286 and dat$PS.389 should be better represented in two columns:
First Column: name of data type - We'll call this column dat$value_type and it will have values that indicate if dat$results comes from PS.230, PS.286, or PS.389.
Second Column: value of data - We'll call this column dat$result and it just shows the value. This will be the y= aesthetic for all plots.
Pre-Processing: Gather into TidyData format
Use the gather() function to gather all columns in to a key ("value_type") and a "value" ("result"). We'll gather all columns except for "dates", so we just note to exclude that column via -dates:
dat <- dat %>% gather(key='value_type', value='result', -dates)
Plot
For the plot, you apply x and y aesthetics to "date" and "result". You can use "value_type" to differentiate based on color and create your legend for points and lines. You also use "value_type" as the column for creating the facets (the three separate plots) via use of facet_grid() function. Note that value_type ~ . arranges by "value_type" vertically, whereas . ~ value_type would arrange horizontally:
ggplot(dat, aes(x=dates, y=result)) +
geom_line(aes(color=value_type)) +
geom_point(aes(color=value_type)) +
scale_x_date(date_breaks = '1 months', date_labels = '%Y-%m') +
facet_grid(value_type ~ .) +
theme_bw()

Resources