I would like to have two barcharts and one linechart in one plot using ggplot.
I checked the following code, but did not work
df1<- data.frame(x=c(1:5), y=c(10,20,30,24,44))
df2<- data.frame(x=c(1:5), y=c(9,25,22,24, 25))
df3<- data.frame(x=c(1:5), y=c(10,20,30,24,44))
ggplot()+
geom_bar(data=df1, aes(x=x, y=y), stat = "identity", position=position_dodge())+
geom_bar(data=df2, aes( y=y), stat="identity", position=position_dodge())+
geom_line(data=df3, aes(x=x, y=y))
Any help would be appreciated.
Abbas
For a start you need to be putting 'data =.., x=..etc' in ggplot and then + the geom_bars and lines.
http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2)/
e.g.
ggplot(data=df1, aes(x=x, y=y)) +
geom_bar(stat = "identity", position=position_dodge())
Related
Assuming I have two data.frames with different data but in the same range of x-values
a <-data.frame(x=c(1,1,1,2,2,2,3,3,3),
y=c(0.3,0.4,0.3,0.2,0.5,0.3,0.4,0.4,0.2),
z=c("do","re","mi","do","re","mi","do","re","mi"))
b <- data.frame(x=c(1,2,3),y=c(10,15,8))
Both, a and b have the same range of X values (1,2,3) but while a is a data.frame with 9 rows, b is a data.frame with 3 rows.
I use geom_bar in order to plot the distribution of values of a, like this:
ggplot(a, aes(x=x, y=y, fill=z)) +
geom_bar(position="stack",stat="identity") +
ylab("") +
xlab("x")
And I use geom_line to plot b data, like this:
ggplot(b, aes(x=x, y=y)) +
geom_line(stat="identity") +
ylab("") + xlab("x") + ylim(0,15)
Now I would like to overlay this geom_line plot to the previous geom_bar plot. My first try was to do the following:
ggplot(a, aes(x=x, y=y, fill=z)) +
geom_bar(position="stack",stat="identity") +
ylab("") + xlab("x") +
ggplot(b, aes(x=x, y=y)) +
geom_line(stat="identity") +
ylab("") + xlab("x") + ylim(0,15)
With no success.
How can I overlay a geom_line plot to a geom_bar plot?
Try this
p <- ggplot()
p <- p + geom_bar(data = a, aes(x=x, y=y, fill=z), position="stack",stat="identity")
p <- p + geom_line(data = b, aes(x=x, y=y/max(y)), stat="identity")
p
Update:
You can rescale the one y to make them the same. As I don't know the relations between the two ys, I rescaled them by using y/max(y). Does this solve you problem?
Try merging the datasets first, then plotting, like this:
require(ggplot2)
df <- merge(a,b,by="x")
ggplot(df, aes(x=x, y=y.x, fill=z)) +
geom_bar(position="stack",stat="identity") +
geom_line(aes(x=x, y=y.y)) +
ylab("") + xlab("x")
Output:
I edited the sample data to better illustrate the effects, because the y-axis scaling of the original data would not have matched well:
a <-data.frame(x=c(1,1,1,2,2,2,3,3,3),
y=c(0.3,0.4,0.3,0.2,0.5,0.3,0.4,0.4,0.2),
z=c("do","re","mi","do","re","mi","do","re","mi"))
b <- data.frame(x=c(1,2,3),y=c(.4,1,.4))
How can I make this plot:
library(ggplot2)
library(data.table)
dt <- data.table(x=1990:2001, y=rnorm(5), z=c("a","b","c"))
ggplot(dt, aes(x=x, y=y, col=z)) +
geom_line()
execpt with the lines all in the same color? I don't know whether there is another aes argument to use or something else to do.
This doesn't do anything:
ggplot(dt, aes(x=x, y=y, col=z), col="black") +
geom_line()
And this merges all the lines into one:
ggplot(dt, aes(x=x, y=y, col=z)) +
geom_line(col="black")
If you just want separate lines for every distinct z you can use the group argument
ggplot(dt, aes(x=x, y=y, group=z)) + geom_line()
If you want to give all lines a color, but the same you need to specify it outside of the aesthetics like this
ggplot(dt, aes(x=x, y=y, group=z)) + geom_line(color = "blue")
When using a discrete values ggplot2 provides a gridline at the tick value at the centre of the value
library(reshape2)
ggplot(data=tips, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(stat="identity", position=position_dodge())
How can I set the grid line from the x axis to appear between the discrete values (i.e. between 'Dinner' and 'Lunch')
I have tried to set panel.grid.minor.x however (I think) as it is discrete this does not work ... this is not a minor value for it to plot the girdline on.
ggplot(data=tips, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(stat="identity", position=position_dodge()) +
theme(panel.grid.minor.x = element_line())
You can add a vertical line that will act as a grid line as follows:
geom_vline(xintercept=1.5, colour='white')
You can, of course, alter line width, colour, style, etc. as needed, and add multiple lines in the appropriate locations if you have several groups of bars that need to be separated by grid lines. For example, using some fake data:
set.seed(1)
dat = data.frame(total_bill=rnorm(100,80,10),
sex=rep(c("Male","Female"),50),
time=sample(c("Breakfast","Brunch","Lunch","Afternoon Tea","Dinner"),
100, replace=TRUE))
dat$time = factor(dat$time,
levels=c("Breakfast","Brunch","Lunch","Afternoon Tea","Dinner"),
ordered=TRUE)
ggplot(data=dat, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(stat="identity", position=position_dodge()) +
geom_vline(xintercept=seq(1.5, length(unique(dat$time))-0.5, 1),
lwd=1, colour="black")
Had the same problem. My solution was to make the grid line bigger ..
set.seed(1)
dat = data.frame(total_bill=rnorm(100,80,10),
sex=rep(c("Male","Female"),50),
time=sample(c("Breakfast","Brunch","Lunch","Afternoon Tea","Dinner"),
100, replace=TRUE))
dat$time = factor(dat$time,
levels=c("Breakfast","Brunch","Lunch","Afternoon Tea","Dinner"),
ordered=TRUE)
ggplot(data=dat, aes(x=time, y=total_bill, color=sex)) +
geom_point(stat="identity", position=position_dodge()) +
theme(panel.grid.major.x = element_line(color = "white", size = 20))
data=data.frame(x=rep(0:9, each=2))
ggplot(data, aes(x=factor(x))) + geom_bar(alpha=0.5) +
geom_point(data=data.frame(x=0:10, y=2), aes(x=factor(x), y=y), alpha=0.5)
ggplot(data, aes(x=factor(x))) + geom_bar(alpha=0.5) +
geom_point(data=data.frame(x=0:10, y=2), aes(x=factor(x), y=y), alpha=0.5) +
scale_x_discrete(limits=0:10)
Also, do I have to factor given x is integer so it is discrete already?
Wrong order
Wrong x axis label.
ggplot(data, aes(x=x)) + geom_bar(alpha=0.5) + scale_x_discrete(limits=0:10) +
geom_point(data=data.frame(x=0:10, y=2), aes(x=x, y=y), alpha=0.5)
You can force a discrete scale to get what you want. It is odd how when you mix geom_point() and geom_bar() ggplot starts ordering things in unexpected ways.
The last image in this blog post.
I have tried searching "nested bar graph" and "hierarchical bar graph", but they may not be the word for it.
Use ggplot and create separate layers:
library(ggplot2)
set.seed(1)
stupid <- data.frame(
group= LETTERS[1:5],
men = sample(1:10, 5),
women = sample(1:10, 5)
)
# Melt the data and calculate totals
mstupid <- melt(stupid, id.vars="group")
stupidTotal <- ddply(mstupid, .(group), summarize, value=sum(value))
ggplot() +
geom_bar(data=stupidTotal, aes(x=group, y=value), fill="grey50") +
geom_bar(data=mstupid, aes(x=group, y=value, fill=variable),
stat="identity", position="dodge") +
theme_bw()
Look for 'barNest' in package plotrix
Use this:
ggplot() +
geom_bar(data=stupidTotal, aes(x=group, y=value, fill="grey50"), stat="identity") +
geom_bar(data=mstupid, aes(x=group, y=value, fill=variable),
stat="identity", position="dodge") +
theme_bw()