I am trying to plot to show the average number of bikes rent on weekday by hourly using ggplot as shown below in R. Since I have weekday as number in my data, its giving text legend as numbers.
ggplot(bike_share_train, aes(x=hour, y=count, color=day))+
geom_point(data = day_summary_by_hour, aes(group = day))+
geom_line(data = day_summary_by_hour, aes(group = day))+
ggtitle("Bikes Rent By Weekday")+scale_colour_hue('Weekday')
I want to change those numbers to weekday names and I tried using below command, but legend is missing in ggplot. Please let me know how to fix this issue.
ggplot(bike_share_train, aes(x=hour, y=count, color=day))+geom_point(data = day_summary_by_hour, aes(group = day)) +
geom_line(data = day_summary_by_hour, aes(group = day))+
ggtitle("Bikes Rent By Weekday")+
scale_colour_hue('Weekday', breaks = levels(bike_share_train$day), labels=c('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'))
Although the OP found the solution, I'm proposing an alternative approach when each group consist of only one observation.
library(ggplot2)
# create some sample data
bike_share_data<-data.frame(hour = c(1.5,2.3,1.3,2.2,1.5),
count = c(21,26,30,15,20),
day = c("1","2","3","4","5"))
head(bike_share_data)
ggplot(bike_share_data, aes(x=hour, y=count, color=day))+
geom_point(data = bike_share_data, aes(group = 1))+
geom_line(data = bike_share_data, aes(group = 1))+
ggtitle("Bikes Rent By Weekday")+
scale_colour_hue('Weekday',
breaks = levels(bike_share_data$day),
labels=c('Monday','Tuesday','Wednesday','Thursday','Friday')
)
My sincere apologies to everyone for my mistake. I extracted the weekday, hour from datetime value but forgot to change the datatype from char to factor for these fields. So, issue in ggplot. I have changed the datatype and checked the ggplot. Now legends are showing.
Related
I'm trying to plot two lines using flight data I gathered. My problem is that after trying different formulas, R is still only showing one line. I've separated my data according to regions (see image below). Can someone help me out with my formula?
If you need any additional information don't hesitate to ask, this is my first time posting on this channel.
ggplot(ica.vs.total, aes(x = Year, y = flights)) +
geom_line(aes(color = region, group = region), size = 1) +
theme_minimal()
When I enter :
library(ggplot2)
ica.vs.total = data.frame(flights=c(215947,197757,185782,201023,279218,261045,213343,205609),
region=c('TotalFlights','TotalFlights','TotalFlights','TotalFlights',
'TotalFlightsICA','TotalFlightsICA','TotalFlightsICA','TotalFlightsICA'),
Year=c(2008,2009,2010,2011,2000,2001,2002,2003))
g = ggplot(ica.vs.total, aes(x = Year, y = flights)) +
geom_line(aes(color = region, group = region), size = 1)+
theme_minimal()
print(g)
I get the expected result :
Double check your code.
I am a newbie in R and would like to seek your advice regarding visualization using reorder_within, and scale_x_reordered (library: tidytext).
I want to show the data (ordered by max to min) by states for each year. This is sample data for illustrative purposes.
test <- data.frame(stateabb = rep(state.abb, each = 5, times = 1),
year = seq(2001,2005,1),
value = sample(1:100, 250, replace = TRUE))
I successfully created the simple chart by state and year by using the following code.
ggplot(test, aes(x = stateabb, y = value)) +
geom_bar(stat = "identity") +
facet_grid(year ~ ., scales = "free_x")
Looking at this chart, it is very hard to see which State is the best in each year. So, I decided to order the value each year by using reorder_within.
ggplot(test, aes(x = reorder_within(stateabb, -value, year), y = value)) +
geom_bar(stat = "identity") +
facet_grid(year ~ ., scales = "free_x") +
scale_x_reordered()
However, I could not show it as I did in the first picture. I thought scale_x_reordered could solve it, but it did not turn out as I expected. I also understand that I need to set the x-axis free in order to show the order of states in each year. But doing that does not get me anywhere. What did I do wrong here? Is there any other appropriate way to show the order of these states by year? Any suggestions or advice to show this chart properly would be appreciated. Thank you so much ahead of time!
This can't work, because facet_grid would only have one shared x-axis. But the orders are different in every facet. You want facet_wrap. For example like this:
library(ggplot); library(tidytext)
ggplot(test, aes(x = reorder_within(stateabb, -value, year), y = value)) +
geom_bar(stat = "identity") + scale_x_reordered() +
facet_wrap(year ~ ., ncol = 1, scales = "free_x", strip.position = "right")
I need to plot hourly data for different days using ggplot, and here is my dataset:
The data consists of hourly observations, and I want to plot each day's observation into one separate line.
Here is my code
xbj1 = bj[c(1:24),c(1,6)]
xbj2 = bj[c(24:47),c(1,6)]
xbj3 = bj[c(48:71),c(1,6)]
ggplot()+
geom_line(data = xbj1,aes(x = Date, y= Value), colour="blue") +
geom_line(data = xbj2,aes(x = Date, y= Value), colour = "grey") +
geom_line(data = xbj3,aes(x = Date, y= Value), colour = "green") +
xlab('Hour') +
ylab('PM2.5')
Please advice on this.
I'll make some fake data (I won't try to transcribe yours) first:
set.seed(2)
x <- data.frame(
Date = rep(Sys.Date() + 0:1, each = 24),
# Year, Month, Day ... are not used here
Hour = rep(0:23, times = 2),
Value = sample(1e2, size = 48, replace = TRUE)
)
This is a straight-forward ggplot2 plot:
library(ggplot2)
ggplot(x) +
geom_line(aes(Hour, Value, color = as.factor(Date))) +
scale_color_discrete(name = "Date")
ggplot(x) +
geom_line(aes(Hour, Value)) +
facet_grid(Date ~ .)
I highly recommend you find good tutorials for ggplot2, such as http://www.cookbook-r.com/Graphs/. Others exist, many quite good.
The left image is my current graph and I would like to make it look like the right one. I'm having two problems. The first is even if I used step in the plot, it doesn't graph the line connecting each dots. The second problem is while the right graph's plots are scattered relatively to the year, mine is scattered proportionally throughout the whole x-axis.
Here is my code
ggplot() +
geom_step(data = tbl, mapping = aes(x = tbl$date, y = tbl$size)) +
geom_point(data = tbl, aes(x = tbl$date, y = tbl$size)) +
labs(x = 'Data', y = 'Size (Kilobytes)', title = 'stringr: timeline of version sizes')
I have to somehow convert current date format(yyyy-mm-dd) and change it to just yyyy format but doing that so would make some points to be in the same year. For example, the first three dates I have are 2009-11, 2009-11, and 2010-02 so if I change the format of year, two of them will be on same spot. And I don't know how to figure this out since I am still trying to learn how to use R.
Thank you in advance!
It takes some finagling with the date, but all you should have to do is add a function from the scalespackage to set your x-axis scale. It requires your time to be as class POSIXct. Used some dummy data since you didn't post any.
library(ggplot2)
library(scales)
library(zoo)
tbl$date <- as.POSIXct(as.yearmon(tbl$date, format = "%Y-%m"))
ggplot() +
geom_step(data = tbl, mapping = aes(x = date, y = size)) +
geom_point(data = tbl, aes(x = date, y = size)) +
labs(x = 'Data', y = 'Size (Kilobytes)', title = 'stringr: timeline of version sizes') +
scale_x_datetime(labels = date_format("%Y"))
Using ggplot2 I want to draw a line that changes colour after a certain date. I expected this to be be simple, but I get a break in the line at the point the colour changes. Initially I thought this was a problem with group (as per this question; this other question also looked relevant but wasn't quite what I needed). Having messed around with the group aesthetic for 30 minutes I can't fix it so if anybody can point out the obvious mistake...
Code:
require(ggplot2)
set.seed(1111)
mydf <- data.frame(mydate = seq(as.Date('2013-01-01'), by = 'day', length.out = 10),
y = runif(10, 100, 200))
mydf$cond <- ifelse(mydf$mydate > '2013-01-05', "red", "blue")
ggplot(mydf, aes(x = mydate, y = y, colour = cond)) +
geom_line() +
scale_colour_identity(mydf$cond) +
theme()
If you set group=1, then 1 will be used as the group value for all data points, and the line will join up.
ggplot(mydf, aes(x = mydate, y = y, colour = cond, group=1)) +
geom_line() +
scale_colour_identity(mydf$cond) +
theme()