Reputation: 21
I'm looking at behavior of different groups of people (called Clusters
in this data set) and their preference for the type of browser they use. I want to create a bar graph that shows the percentage of each cluster that is using each type of browser.
Here is some code to generate a similar dataset (please ignore that the percentages for each cluster will not add up to 1):
browserNames <- c("microsoft","mozilla","google")
clusterNames <- c("Cluster 1","Cluster 2","Cluster 3")
percentages <- runif(n=length(browserNames)*length(clusterNames),min=0,max=1)
myData<-as.data.frame(list(browserNames=rep(browserNames,3),
clusterNames=rep(clusterNames,each=3),
percentages=percentages))
Here's the code I've been able to come up with so far to get the graph I desire:
ggplot(myData, aes(x=browserNames, y=percentages, fill=factor(clusterNames))) +
geom_bar(stat="identity",position="dodge") +
scale_y_continuous(name="Percent Weight", labels=percent)
I want the fill for each cluster to be a gradient fill with high and low values that I determine. So, in this example, I would like to be able to set 3 high and low values for each cluster that is represented.
I've had trouble with the different scale_fill commands, and I'm new enough to ggplot that I am pretty sure I'm probably just doing it wrong. Any ideas?
Edit: Here is a picture of what I'm looking for:
(Original image available at https://www.dropbox.com/s/py6hifejqz7k54v/gradientExample.bmp)
Upvotes: 2
Views: 1780
Reputation: 59355
Is this close to what you had in mind??
# color set depends on browser
library(RColorBrewer) # for brewer.pal(...)
gg <- with(myData, myData[order(browserNames,percentages),])
gg$colors <- 1:9
colors <- c(brewer.pal(3,"Reds"),brewer.pal(3,"Greens"),brewer.pal(3,"Blues"))
ggplot(zz, aes(x=browserNames, y=percentages,
fill=factor(colors), group=factor(clusterNames))) +
geom_bar(stat="identity",position="dodge", color="grey70") +
scale_fill_manual("Cluster", values=colors,
breaks=c(3,6,9), labels=c("Google","Microsoft","Mosilla"))
# color set depends on cluster
library(RColorBrewer) # for brewer.pal(...)
gg <- with(myData, myData[order(clusterNames,percentages),])
gg$colors <- 1:9
col <- c(brewer.pal(3,"Reds"),brewer.pal(3,"Greens"),brewer.pal(3,"Blues"))
ggplot(gg, aes(x=browserNames, y=percentages,
fill=factor(colors), group=factor(clusterNames))) +
geom_bar(stat="identity",position="dodge", color="grey70") +
scale_fill_manual("Cluster", values=col,
breaks=c(3,6,9), labels=c("Cluster1","Cluster2","Cluster3"))
Upvotes: 3