Using `geom_line()` with X axis being factors

Suppose I have a dataframe:

hist <- data.frame(date=Sys.Date() + 0:13,
                   counts=1:14)

I want to plot the total count against weekday, using a line to connect the points. The following puts points on each value:

hist <- transform(hist, weekday=factor(weekdays(date),
                                       levels=c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')))
ggplot(hist, aes(x=weekday, y=counts)) + geom_point(stat='summary', fun.y=sum)

When I try to connect them with a line (geom_line()), ggplot complains about only having one data observation per group and hence is not able to draw a line between the points.

I understand this - it's trying to draw one line for each weekday (factor level).

How can I get ggplot to just pretend (for the purposes of the line only) that the weekdays are numeric? Perhaps I have to have another column day_of_week that is 0 for monday, 1 for tuesday, etc?

Answers


If I understand the issue correctly, specifying group=1 and adding a stat_summary() layer should do the trick:

ggplot(hist, aes(x=weekday, y=counts, group=1)) +
geom_point(stat='summary', fun.y=sum) +
stat_summary(fun.y=sum, geom="line")


Need Your Help

android How to find the difference in m between tow point in maps if i have the Longitude and Latitude?

android google-maps

I want to find the distance between two point in map if i have the two point should i use the linear equation or should i Interest of radius Something like this

AutoSize text in a fixed-sized label to avoid text jumping to second line

c# string winforms label autosize

I have a label with a fixed size, approximately 100 pixel width and 20 pixel height.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.