I have multiple csv files with same structure of data
url, A,B,C,D
a.com,1,2,3,4
b.com,3,4,5,6
I can create a stacked bar plot with urls on x-axis and A,B,C,D stacked on top of each other.
Now I want to create clustered stacked bar plots, with multiple such csv files, all indexed by the same url on the x axis.
data1 = read.csv("data.csv")
data2 = read.csv("data2.csv")
data.m = melt(data1, id.var="url")
ggplot(data.m, aes(x = url, y = value, fill = variable)) +
geom_bar(position="fill",stat = "identity")
Basically add data2 to the plot. Not sure if I am supposed to use gather or facets or manually create new columns post melt?
It should look something like this:
Is this what you're after?
# Two sample datasets
df1 <- cbind.data.frame(
url = c("a.com", "b.com"),
A = c(1, 3), B = c(2, 4), C = c(3, 5), D = c(4, 6));
df2 <- cbind.data.frame(
url = c("a.com", "b.com"),
A = c(5, 7), B = c(6, 8), C = c(7, 9), D = c(8, 10));
Using gather
# Using gather
require(tidyr);
df <- rbind.data.frame(
gather(cbind.data.frame(df1, src = "df1"), variable, value, -url, -src),
gather(cbind.data.frame(df2, src = "df2"), variable, value, -url, -src));
Using melt
# Using melt
require(reshape2);
df <- rbind.data.frame(
melt(cbind.data.frame(df1, src = "df1"), id.vars = c("url", "src")),
melt(cbind.data.frame(df2, src = "df2"), id.vars = c("url", "src")));
Sample plot
ggplot(df, aes(x = url, y = value, fill = variable)) + geom_bar(stat = "identity") + facet_wrap(~ src);
Note: If you have multiple csv
files, best to df.list <- lapply(..., read.csv)
, and then melt
df.list
to get columns variable
, value
and L1
(which corresponds to src
).
Update
I'm not entirely clear on what you are after, so this is a bit of a stab in the dark. You can also cluster by url
(instead of src
):
ggplot(df, aes(x = src, y = value, fill = variable)) + geom_bar(stat = "identity") + facet_wrap(~ url);
and/or show bars side-by-side (instead of stacked)
ggplot(df, aes(x = src, y = value, fill = variable)) + geom_bar(stat = "identity", position = "dodge") + facet_wrap(~ url);