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)
Related
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
When I plot using ggplot I get grey vertical lines on my plot before data chart. Any ideas on how to remove it would be highly appreciated.
ggplot(fitbit_data, aes(x = Date, y = Steps)) +
geom_bar(stat = "identity", fill = "green") +
labs(title = "My Steps", subtitle = " June - Dec 2019",
x = " Date", y = "Steps") +
scale_x_date(
date_labels = "%b\n%Y",
date_breaks = "1 month",
limits = c(as.Date("2019-06-01"), as.Date("2019-12-31"))
)
Likely the data is converted to factor, thus ggplot shows a categorical y-axis, that then appears with overlapping labels that look like those grey columns.
When reading the data make sure to use
df= read.table(...,
# assign appropriate data types by using
colClasses = c(...),
... ,
# it can also be adviseable to use
stringsAsFactors = FALSE)
I am trying to understand how the limit of axis works in ggplot histogram. When I tried to set the limit, the histogram shifted.
Below is a script to generate histogram of kWh for each month. On the first graph (p1) I did not set the axis limit. On the second graph (p2) I set the limit, which is the same as the time span of the dataset.
The histograms are attached. On the first histogram, the first bar is correct, for Jan and the value is above 20000.
The problem is: on the second histogram, the first bar is actually for Feb (see the value is below 20000, which is wrong), but the bar in the histogram says Jan.
My question: what causes this time shift? Furthermore, two bars are missing in the second histogram.
Any help would be appreciated. Thanks
Lines <- "year,month,kWh
2014,1,21535
2014,2,19826
2014,3,20796
2014,4,21249
2014,5,21442
2014,6,22685
2014,7,20569
2014,8,23346
2014,9,23440
2014,10,20148
2014,11,24415
2014,12,21628"
con <- textConnection(Lines)
df = read.csv(con)
df$timestamp = as.POSIXct(paste(df$year,df$month,"01", sep="-"),
format="%Y-%m-%d", tz="GMT")
library(ggplot2)
png(filename = "p1.png", width = 800, height = 400, units = "px", pointsize = 24 )
p1 = ggplot(data = df, aes(x = timestamp, kWh)) +
geom_bar(stat = "identity") +
scale_x_datetime(date_breaks = "1 month",
date_labels = "%m\n%Y")
p1
png(filename = "p2.png", width = 800, height = 400, units = "px", pointsize = 24 )
time.start=as.POSIXct("2014-01-01",format="%Y-%m-%d", tz="GMT")
time.end=as.POSIXct("2014-12-01",format="%Y-%m-%d", tz="GMT")
p2 = ggplot(data = df, aes(x = timestamp, kWh)) +
geom_bar(stat = "identity") +
scale_x_datetime(limits = c(time.start,time.end),
date_breaks = "1 month",
date_labels = "%m\n%Y")
p2
I am a beginner in ggplot2. I am unable to use date_minor_breaks to show quarterly "ticks" on x-axis.
Here's my code:
x<-c(seq(1:12))
time<-c("2010Q1","2010Q2","2010Q3","2010Q4","2011Q1","2011Q2", "2011Q3","2011Q4","2012Q1","2012Q2","2012Q3","2012Q4")
z<-data.frame(type = x,time = time)
z$time = as.yearqtr(z$time)
z$time = as.Date(z$time)
ggplot(data = z, aes(x=time,y=type)) +
geom_point() +
scale_x_date(date_labels = "%Y",date_minor_breaks = "3 months",name = "Year") +
theme_tufte() +
theme(legend.position = "none")
I researched this topic on SO Formatting dates with scale_x_date in ggplot2 and on https://github.com/hadley/ggplot2/issues/542, and found that there were some issues reported on this topic. However, I didn't quite follow the conversation about changes to ggplot2 because it's been only 6 days since I started using ggplot2.
Here's the graph I got (it doesn't have any ticks)...
Here's a sample graph with "tick marks" generated from Excel. Please ignore values because my point of creating this Excel chart is to demonstrate what I am looking for--i.e. "quarterly ticks". I'd appreciate your help.
You may have to make major breaks every three months and then pad your labels with blanks to give the illusion of major (labeled) and minor (unlabeled) ticks. See this answer for another example.
First manually make the breaks for the tick marks at every quarter.
breaks_qtr = seq(from = min(z$time), to = max(z$time), by = "3 months")
Then make the year labels and pad these labels with three blanks after each number.
labels_year = format(seq(from = min(z$time), to = max(z$time), by = "1 year"), "%Y")
labs = c(sapply(labels_year, function(x) {
c(x, rep("", 3))
}))
Now use the breaks and the labels with the labels and breaks arguments in scale_x_date. Notice that I'm not using date_labels and date_breaks for this.
ggplot(data = z, aes(x=time,y=type)) +
geom_point() +
scale_x_date(labels = labs, breaks = breaks_qtr, name = "Year") +
theme_tufte() +
theme(legend.position = "none")
You should also define your (major) date breaks:
ggplot(data = z, aes(x=time, y=type)) +
geom_point() +
scale_x_date(date_breaks = "1 year", name = "Year", date_minor_breaks="3 months",
limits = c(as.Date(as.yearqtr("2009Q4")),
as.Date(as.yearqtr("2013Q2"))),
expand=c(0,0), date_labels = "%Y") +
theme(legend.position = "none")
And some other "fancy" stuff to align the minor ticks with the major ticks (I guess there a better ways to do this, but this works).
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"))