Dario Lacan
Dario Lacan

Reputation: 1140

How to plot multiple lines between two factor variables

I'm trying to produce with R a specific kind of diagram.

graph)

This is a description of the diagram that I am trying to obtain: The variable ("Date") on the x-axis is a factor. Its levels are reported on the x-axis. Also the variable on the y-axis ("Theme") is a factor, and what is plotted is the proportion of its counts for each "Date" level.

So for example let's consider this database:

    data <- data.frame(ID = 1:20, Date = as.factor(c(1,1,1,1, 1, 1,1, 2,2,2,2, 2, 2, 2, 2, 3,3,3, 3, 3)), Theme = as.factor(c("a","b", "b", "c", "c", "c", "c","a","a","a", "a", "b","c", "c","a","a","b","b", "b", "c")))

In R, I managed to produce a stacked bar plot, counting the proportion of Theme values for each Date:

    ggplot(data, aes(x = Date)) + geom_bar(aes(fill = Theme), position = 'fill')

My aim is to produce three lines connecting horizontally, for each x-axis level ("Date"), the proportion of counts of the y-axis variable ("Theme").

I found on the Web one way that could do, but I did not manage to make them work.

    ggplot(Theme, aes(Date)) + geom_freqpoly(aes(group = Theme, colour = Theme))

(I found this formula here: http://docs.ggplot2.org/0.9.3.1/geom_bar.html The 13th diagram is basically what I am looking for.)

Thanks for any help!

Upvotes: 4

Views: 3969

Answers (2)

Dario Lacan
Dario Lacan

Reputation: 1140

Thanks to both Simon O'Hanlon and df239.

In the end I managed to obtain what I wanted with this formula:

ggplot(data, aes(Date)) + geom_freqpoly(aes(group = Theme, colour = Theme))

I'm sharing it for others who may land on this page.

Consider that if you declare a variable for the y axis, such as aes(Date, Theme), it will not work!

Upvotes: 0

df239
df239

Reputation: 471

For your data, the closest approximation looks like the following:

ggplot(data, aes(x=Date, y=ID, group=Theme, col=Theme)) + geom_line() + geom_point()

As for the dates, a better approach would be to use standard POSIXct or Date classes, they can be visualised by the same ggplot expression and the axis can be further customised by adding scale_x_date() or scale_x_datetime functions.

Upvotes: 1

Related Questions