unable changing linetype ggplot2 - r

I would like to change color and line type in my ggplot. I am using this code:
a <- runif(84, 20, 80)
a<-ts(a,start = 2009,frequency = 12)
#a<-ts(result$`dataset1$Summe`,start = 2009,frequency = 12)
a3 <- zoo(a, order.by = as.Date(yearmon(index(a))))
p1 <- autoplot(a3)
p1 + scale_x_date(labels = date_format("%m/%Y"),breaks = date_breaks("2 months"), limits = as.Date(c('2009-01-01','2017-08-01')))+ theme(axis.text.x = element_text(angle = 90))+ theme(axis.text.x = element_text(angle = 90))+
labs(x = "Date",y="Test") + theme(panel.background = element_rect(fill = 'white', colour = 'black'))+geom_line(linetype="dotted", color="red")
but only the color is changed. What should I do to change line type?

autoplot() will pick a sensible default for the object it's passed. If you want to customize its appearance it's probably better to use the standard ggplot() function.
To be able to do that the zoo object should be passed trough fortify():
ggplot(fortify(a3, melt = TRUE)) +
geom_line(aes(x = Index, y = Value), linetype='dashed', color="red") +
scale_x_date(labels = date_format("%m/%Y"),
breaks = date_breaks("2 months"),
limits = as.Date(c('2009-01-01','2017-08-01')))+
theme(axis.text.x = element_text(angle = 90),
axis.text.x = element_text(angle = 90),
panel.background = element_rect(fill = 'white', colour = 'black'))+
labs(x = "Date",y="Test")
(NB: the dashed line at the top is caused by the panel.background theme option)

Related

wrong dates at x axis when plotting POSIXct with scale_x_datetime

From the following dataframe df:
df <- data.frame(Date=as.POSIXct(c("2002-07-01","2002-06-01","2002-05-01","2002-04-01","2002-03-01")),
Cat1=c(1,0,1,0,0),
Cat2=c(1,1,1,0,0),
Cat3=c(0,1,1,0,1),
Cat4=c(0,0,1,1,0))
df <- tidyr::pivot_longer(df, -1)
When using scale_x_datetime this require POSIXct class object, otherwise this complaints with error.
Error: Invalid input: time_trans works with objects of class POSIXct
only
ggplot(df,
aes(x = Date, y = factor(name, levels = rev(unique(name))),
fill = as.factor(value))) +
geom_tile(color = "black") +
scale_fill_manual(values = c("white", "grey50")) +
theme_void() +
theme(legend.position = "none",
axis.text = element_text(size = 8),
plot.margin = margin(20, 20, 20, 20),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, size =5))+
scale_x_datetime(date_breaks = "1 month",
labels = scales::date_format("%m-%Y"))
With the following input:
By inspecting the range of Date column, this does not match with plot labels from x-axis.
range(df$Date)
Does anyone how to solve this?
Use date class, and scale_x_date
library(ggplot2)
# changing the class by constructing your data differently
df <- data.frame(Date=as.Date(c("2002-07-01","2002-06-01","2002-05-01","2002-04-01","2002-03-01")),
Cat1=c(1,0,1,0,0),
Cat2=c(1,1,1,0,0),
Cat3=c(0,1,1,0,1),
Cat4=c(0,0,1,1,0))
df <- tidyr::pivot_longer(df, -1)
ggplot(df,
aes(x = Date, y = factor(name, levels = rev(unique(name))),
fill = as.factor(value))) +
geom_tile(color = "black") +
scale_fill_manual(values = c("white", "grey50")) +
theme_void() +
theme(legend.position = "none",
axis.text = element_text(size = 8),
plot.margin = margin(20, 20, 20, 20),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, size =5))+
scale_x_date(labels = scales::date_format("%m-%Y"))
Created on 2021-11-02 by the reprex package (v2.0.1)

Why is the x-axis text size not getting bigger with axis.text.x in R?

I am trying to increase the font size of the tick numbers of the x-axis, but is not recognised by theme(), why is it?
ARe functions overriding each other?
df <- data.frame('Variable'=c(1,1,2,2,2,3,3,4,4,4,5,6,6,6,7,7,7),
'value'=c(10,11,12,14,12,14,16,12,18,21,19,23,24,25,26,27,25))
ggplot(df, aes(x = Variable, y = value)) +
geom_jitter(alpha = 0.8, color = "tomato") +
theme(axis.text.x = element_text(size = 25,color="black"),
axis.text.y = element_text(size = 12,color="black" ))+
theme_classic()+ scale_x_continuous(breaks = 1:7)
Edit : stefan was quicker in the comment section.
Put the theme() after theme_classic()
ggplot(df, aes(x = Variable, y = value)) +
geom_jitter(alpha = 0.8, color = "tomato") +
scale_x_continuous(breaks = 1:7) +
theme_classic() +
theme(axis.text.x = element_text(size = 25,color="pink"),
axis.text.y = element_text(size = 12,color="black" ))

Overlapping labels in legend in ggplot?

How to avoid overlapping the lagend labels in ggplot?
ggplot(g3, aes(variable, country, fill= value)) + geom_tile() +
theme(axis.text.y = element_text(size = 15), axis.text.x = element_text(angle = 45, hjust = 1, size = 15), legend.position="bottom", legend.text = element_text(size = 15, angle = 50),
axis.title.x = element_blank(),axis.title.y = element_blank())
For axis labels, the best way to fix this is to use the ggplot2::guide_axis() function, which will adjust both the angle and vertical/horizontal positioning at the same time. You can also make your legend labels easier to read by scaling the label. This might obviate the need to rotate the legend labels. For example:
library(ggplot2)
ggplot(g3, aes(variable, country, fill = value)) +
geom_tile() +
labs(x = "", y = "") +
scale_fill_continuous(labels = scales::label_number(scale = 10000, suffix = "k")) +
guides(x = guide_axis(angle = 45), fill = guide_colorbar(direction = "horizontal)) +
theme(legend.position = "bottom", text = element_text(size = 15))

Axis Transformation in ggplot for 3-axis graphs

I'm trying to transform my primary axis to make all the data readable in relation to the secondary axis. Here's the code I'm using:
ggplot(data = NOAARainfallAirTemp, aes(x = Date)) +
geom_smooth(aes(y = Temp, color ="Temperature"), method = "loess", span = 0.3, fill = "blue") +
geom_point(aes(y = Temp, color = "Temperature"), alpha = 0.3) +
geom_smooth(aes(y = PRCP*6, color = "Precipitation"), method = "loess", span = 0.3, fill = "orange") +
geom_point(aes(y = PRCP*6, color = "Precipitation"), alpha = 0.3) +
scale_x_date(expand = c(0.001,0.001), date_breaks = "3 months", date_labels = "%b%Y", date_minor_breaks = "1 month") +
scale_y_continuous(expand = c(0.001,0.001), sec.axis = sec_axis(~./6, name = "Precipitation")) +
ylab("Temperature (°C)") +
ggtitle("Diagnostics of NOAA Data") +
theme(axis.text.x=element_text(angle = 90, hjust = 0), plot.title = element_text(hjust = 0.5)) +
guides(shape = guide_legend(override.aes = list(shape = 15)), color = guide_legend(override.aes = list(fill = NA))) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_line(colour = "grey"),panel.background = element_blank(), axis.line = element_line(colour = "black"))
The code makes this graph:
As you can see, it's hard to make out trends, especially for the precipitation data. Is there a way to "spread out" the data for the primary axis or is there a better way of going about this?
Any help will be greatly appreciated!

Drawing a timeline with denoted time periods AND annotated events in ggplot2

I'm trying to use ggplot2 to create a timeline with annotated events. This is my data:
cambodia = data.frame(Period = c("Funan", "Chenla/Zhenla","Khmer Empire","Dark Ages of Cambodia"),StartDate = c(-500,550,802,1431), EndDate = c(550,802,1431,1863))
cambodia.events = data.frame(Event = c("Migration of peoples from southeastern China\ninto Cambodia"), Date=c(50), disloc = c(1))
This is the code that I'm using:
library(ggplot2)
library(viridis)
library(ggthemes)
ggplot(data=cambodia) +
geom_segment(aes(x=StartDate, xend=EndDate, y=0., yend=0., color=Period) , linetype=1, size=4) +
scale_color_viridis(discrete = TRUE)+
scale_y_continuous(limits=c(0,0.5))+
scale_x_continuous(limits=c(-500,1863), breaks= c(seq(0,1863,by=1863), cambodia$StartDate, cambodia$EndDate))+
xlab("Time")+
ylab("Periods of History")+
theme_minimal() + theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.title.y=element_blank(),axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
theme(aspect.ratio = .2)+
theme(legend.position="none") +
geom_text(aes(x=StartDate-100 + (EndDate- StartDate)/2,y=0.05,label=Period,angle=25,hjust=0))
What is produced currently looks fine but it doesn't have any annotated events, as found in this Stack Overflow post. I have tried to add this code from that post:
geom_segment(aes(x = Event,y = disloc,xend = Event),data=cambodia.events,yend = 0) +
geom_segment(aes(x = 900,y = 0,xend = 2050,yend = 0),data=cambodia.events,arrow = arrow(length = unit(x = 0.2,units = 'cm'),type = 'closed')) +
geom_text(aes(x = Event,y = disloc,label = Date),data=cambodia.events,hjust = 1.0,vjust = 1.0,parse = FALSE)
but unsurprisingly, it isn't working (I assume because the arguments are conflicting, but I'm not sure how to resolve them).
As a note: The error it throws up when I try to use the full code above (with the hash lines un-hashed) is "Error: Discrete value supplied to continuous scale."
In your code for the annotation you put x = Event, when on your existing plot Date is on the x-axis, so you just need to make sure that both layers share the same x-axis scale:
ggplot() +
geom_segment(data = cambodia, aes(x = StartDate, xend = EndDate, y = 0, yend = 0, color = Period), linetype = 1, size = 4) +
geom_text(data=cambodia, aes(x=StartDate-100 + (EndDate- StartDate)/2,y=0.05,label=Period,angle=25,hjust=0)) +
scale_color_viridis(discrete = TRUE)+
scale_y_continuous(limits=c(0, 0.5))+
scale_x_continuous(limits=c(-500, 1863), breaks= c(seq(0, 1863, by = 1863), cambodia$StartDate, cambodia$EndDate))+
xlab("Time")+
ylab("Periods of History")+
theme_minimal() +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
axis.title.y = element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
aspect.ratio = .2,
legend.position="none") +
geom_segment(data = cambodia.events, aes(x = Date, xend = Date, y = 0, yend = .25)) +
geom_text(data = cambodia.events, aes(x = Date, y = .35, label = Event))

Resources