I am using ggplot and ggplotly to make a graph with dates on the x axis and rain on the y axis. When I upload the graph to plotly on the web the dates are changed to numeric values.
When I use plot_ly and upload the same graph the date is displayed correctly.
The date is displayed correctly when you hover over a bar in both graphs.
Any ideas how to fix this as I really like using ggplot… Bill
I have included the data and code below:
the following uploads date incorrectly ------
rain_ggplot <- rain.df %>% filter(date > ymd(“2018-02-01”) &
date < ymd(“2018-02-25”)) %>%
ggplot(aes(x = date, y = rain_day_in)) +
geom_bar(stat=“identity”) +
labs(x="", y = “Daily Rainfall (inches)”)+
scale_y_continuous( expand=c(0,0))+
scale_x_datetime(date_breaks = “2 days”,
# limits = as.Date(c(‘2016-02-01’,‘2017-12-31’)),
expand=c(0,0)) + #labels = date_format("%b-%d %H"),
text = paste(‘Rainfall (in)’, rain_day_in,
'
Date: ', as.Date(date)
) +
theme(
# LABLES APPEARANCE
axis.title.x=element_blank(),
axis.title.y=element_text(size=12, face=“bold”),
axis.text.x = element_text(size=12, face=“bold”, angle=45), #
axis.text.y = element_text(size=12, face=“bold”),
# ADD AXES LINES AND SIZE
axis.line.x = element_line(color=“black”, size = 0.3),
axis.line.y = element_line(color=“black”, size = 0.3),
# ADD PLOT BOX
panel.border = element_rect(colour = “black”, fill=NA, size=0.3))
rain_ggplot <- ggplotly(rain_ggplot, originalData = TRUE)
rain_ggplot %>% layout(margin = list(l = 75, b=120))
how to upload from R
Sys.setenv(“plotly_username”=“xxx”)
Sys.setenv(“plotly_api_key”=“xxxx”)
api_create(rain_ggplot, filename = “t2_rain_barplot”)
chart_link = api_create(test, filename=“t2_rain_barplot”)
chart_link
the following works ----
rain <- plot_ly(rain.df, x=~date, y=~rain_day_in, type=“bar”, color=I(“blue”)) %>%
layout(title = “Rain at T2”,
xaxis = list(title = “”),
yaxis = list(title = “Rain (inches)”))
rain
dataframe -
rain.df <- structure(list(site = c(“t2”, “t2”, “t2”, “t2”, “t2”, “t2”, “t2”,
“t2”, “t2”, “t2”, “t2”, “t2”, “t2”, “t2”, “t2”, “t2”, “t2”, “t2”,
“t2”, “t2”), date = structure(c(1517443200, 1517529600, 1517616000,
1517702400, 1517788800, 1517875200, 1517961600, 1518048000, 1518134400,
1518220800, 1518307200, 1518393600, 1518480000, 1518566400, 1518652800,
1518739200, 1518825600, 1518912000, 1518998400, 1519084800), tzone = “UTC”, class = c(“POSIXct”,
“POSIXt”)), rain_day_in = c(0, 0, 0, 0, 0, 0.060000032, 0.010000005,
0, 0.020000011, 0, 0, 0.020000011, 0.090000049, 0, 0.020000011,
0, 0.090000049, 0, 1.040000562, 0.87000047), rain_day_mm = c(0,
0, 0, 0, 0, 1.524, 0.254, 0, 0.508, 0, 0, 0.508, 2.286, 0, 0.508,
0, 2.286, 0, 26.416, 22.098)), .Names = c(“site”, “date”, “rain_day_in”,
“rain_day_mm”), class = c(“tbl_df”, “tbl”, “data.frame”), row.names = c(NA,
-20L))