This is related to : HairEyeColor bar chart in R
I am using following code to produce a similar plot:
mm = melt(HairEyeColor)
ggplot(mm)+geom_bar(aes(x=Hair, y=value, fill=Eye), stat='identity',position='dodge')+facet_grid(Sex~.)
I want to have each bar show hair color in upper part and eye color in lower part. How can this be done? I tried to modify the code in previous question but could not manage. Thanks for your help.
Here it is, I don't think this is a good visualization though...
mm2 <- mm
mm2$value <- mm2$value/4
ggplot(mm)+geom_bar(aes(x=Hair, y=value, fill=Eye), stat='identity',position='dodge')+facet_grid(Sex~.) +
geom_bar(data = mm2, mapping = aes(x=Hair, y=value, fill=Hair, group=Eye), stat = "identity", position = "dodge", colour = "white") +
scale_fill_manual(values = sub("blond", "yellow", sub("hazel", "gold", tolower(sort(unique(c(levels(mm$Eye), levels(mm$Hair))))))))
UPDATE
mm2 <- mm
mm2$value <- mm2$value/2
ggplot(mm)+geom_bar(aes(x=Hair, y=value, fill=Hair, group = Eye), stat='identity',position='dodge', colour = "white")+facet_grid(Sex~.) +
geom_bar(data = mm2, mapping = aes(x=Hair, y=value, fill=Eye, group=Eye), stat = "identity", position = "dodge", colour = "white") +
scale_fill_manual(values = sub("blond", "yellow", sub("hazel", "gold", tolower(sort(unique(c(levels(mm$Eye), levels(mm$Hair))))))))
Related
I want to change the colors of the points to reflect the "fluorophore" column in my data (either red, green, or amber). However, whenever I do this manually using scale_color_manual, it changes the boxplot to be each individual fluorophore color as well. I want the boxplot to be for each well number, but the data points to be colored according to fluorophore!
ggplot(sample1_50kreg, aes(x = well_number, y = cq)) +
geom_boxplot() +
geom_point(color = "purple", alpha = 0.5) +
theme_bw()
[]
ggplot(sample1_50kreg, aes(x = well_number, y = cq, color=fluorophore)) +
geom_point(alpha = 0.5) +
geom_boxplot () +
theme_bw() +
scale_color_manual(breaks = c("red", "green", "amber"),
values = c("gold", "green", "red"))
[]
sample1_50kreg <- as.factor(sample1_50kreg$fluorophore)
and re-run your script
check: http://www.sthda.com/english/wiki/ggplot2-colors-how-to-change-colors-automatically-and-manually
I figured it out!
ggplot(sample1_50kreg, aes(x = well_number, y = cq)) +
geom_boxplot() +
theme_bw() +
geom_point (aes(color = fluorophore)) +
scale_color_manual(breaks = c("red", "green", "amber"),
values = c("gold", "green", "red"))
I'm trying to have this plot:
library(ggplot2)
testdata <- data.frame(x=c(1:5),a=c(1:5),b=c(10:6))
ggplot(testdata, aes(x = x)) +
geom_bar(aes(y=b), stat = "identity", fill="darkgrey")+
geom_bar(aes(y=a), linetype="solid", colour="black", stat = "identity", fill=NA)
with a legend. Since I can't get a legend going here (this would be a nice workaround if you know how), I tried to approach the 'correct' way to plot this in ggplot, namely with long data such as:
testdata <- data.frame(x = c(1:5,1:5), y = c(1:5,10:6), group = c(rep("a",5), rep("b",5)))
ggplot(testdata, aes(x = x, y = y, group = group, fill = group)) +
geom_bar(stat = "identity", linetype = "solid", colour = "black", position = position_dodge(width = 0))+
scale_fill_manual(values = c(NA, "darkgrey"))
While I do have a legend here, the bars are much further apart. The usual parameter to change this is the width inside position_dodge but I need that equal to 0 for the 100% overlay. So my question in the ideal world is: can I decrease the distance of the bars in the second plot? If this is not feasible, can I add a legend to the fist plot? Any help would be appreciated!
Try changing the width outside position_dodge.
ggplot(testdata, aes(x = x, y = y, group = group, fill = group)) +
geom_bar(width=1.5,stat = "identity", linetype = "solid", colour = "black", position = position_dodge(width = 0))+
scale_fill_manual(values = c(NA, "darkgrey"))
How do I change fill pattern or color group by group? My current code only changes the outline; I want to change the fill pattern or fill color group (not outline) by sex but also keep my fill gradient which is by Eye.
colors <- c("Green", "Blue", "Hazel", "Brown")
data <- data.frame(HairEyeColor)
data$Eye <- as.numeric(factor(data$Eye, labels = 1:4))
data <- data[c(5,6,12,15,17,22,27,28), ]
ggplot(data, aes(x = Hair, y = Freq, fill = Eye, group = Sex)) +
geom_bar(stat = "identity", position = position_dodge(), aes(colour = Sex)) +
scale_fill_continuous(low = "blue", high = "green")
Bar plot from ggplot2 package does not support fill pattern at the moment (and as far as i know it is not possible with other packages neither).
However there are few solution that are going to help spot the difference in sex and eye easily which you can consider:
1.Using different (lighter) fill colours,thicker bar boundaries and theme_bw():
ggplot(data, aes(x = Hair, y = Freq, fill = Eye, group = Sex)) +
geom_bar(stat = "identity", position = position_dodge(), aes(colour = Sex), size=2) +
scale_fill_continuous(low = "white", high = "grey") + theme_bw()
Merging two columns: Sex and Eye to get the new factor column which is going to be used as a fill argument:
data$Sex_Eye <- paste(data$Sex, data$Eye, sep="_")
ggplot(data, aes(x = Hair, y = Freq, fill = Sex_Eye)) +
geom_bar(stat = "identity", position = position_dodge()) + theme_bw()
Using geom_jitter() instead of geom_bar() and setting up shape argument as Sex:
ggplot(data, aes(x = Hair, y = Freq, colour = Eye, shape = Sex)) +
geom_jitter(size=5) + scale_colour_continuous(low = "blue", high = "green") + theme_bw()
I want to generate a simple bar plot with ggplot2 with the bars ordered by the y-value and the colours manually defined. Here is what I tried:
df <- data.frame(c("a", "b", "c"), c(2, 3, 1))
colnames(df) <- c("shop", "revenue")
ggplot(data = df, aes(x = reorder(shop, revenue), y = revenue, fill = shop)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("blue", "yellow", "black")) +
theme_minimal()
The problem is: the colours are wrongly ordered (black, blue and yellow instead of blue, yellow and black as stated in scale_fill_manual). How to fix this?
With scale_fill_manual you assign colors to levels in your data.
At the same time, you use reorder(shop, revenue) in the definition of aes, which orders the data from left to right in ascending order. The third and last definition of the color "blue" was assigned to c which is now at the left hand side as it is the smallest.
You could time this to circumvent this:
ggplot(data = df, aes(x = reorder(shop, revenue), y = revenue, fill = shop)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("yellow", "black", "blue")) + # CHANGED
theme_minimal()
Or as #JeroenBoeye suggested:
ggplot(data = df, aes(x = reorder(shop, revenue), y = revenue, fill = shop)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("c" = "blue", "a" = "yellow", "b" = "black")) + # Jeroen Boeye's suggestion
theme_minimal()
Please let me know whether this solves your problem.
I want to change the order of the bar plot only for the last set, just to highlight it. I used scale_fill_manual(), but it didn't help much.
Here's my code:
x<-c(rep(c("Type1", "Type2"),4))
y<-c(4,5,6,7,3,4,5,2)
time<-c(2010,2010,2011,2011,2012,2012,2013,2013)
z<-data.frame(type = x, val=y, Time = time)
ggplot(data = z, aes(x=Time,y=val)) +
geom_bar(stat = "identity", position = "dodge", aes(fill=type))+
scale_fill_manual(values = c(rep(c("white", "gray51"),3),"white","red"))
Here's the output:
I want the graph to look like:
Is there any way I can do this? I would appreciate any help. I looked at change color of only one bar in ggplot but it doesn't seem to be about grouped data.
My general mantra is that ggplot is very good at plotting the data you give it. If you want it to plot something different, the easiest way is usually to modify your data.
z$type2 = as.character(z$type)
z$type2[z$type == "Type2" & z$Time == 2013] = "Type2 "
I added a sneaky extra space in "Type2 " for the row you want to highlight. It will be a distinct factor level and get its own color (and even be coerced into the a nice order using the alphabetical default). But it will appear the same in the legend label.
ggplot(data = z, aes(x=Time,y=val)) +
geom_bar(stat = "identity", position = "dodge", aes(fill=type2))+
scale_fill_manual(values = c("white", "gray50", "red"))
I thought that omitting the red from the legend would be difficult, but this answer showed me that all that is needed is to add breaks = c("Type1", "Type2") as an argument to scale_fill_manual.
What about highlighting the bar with a border. For example:
z$hi = with(z, ifelse(type=="Type2" & Time==2013, "Y","N"))
ggplot(data = z, aes(x=Time,y=val)) +
geom_bar(stat = "identity", position = "dodge",
aes(fill=type, colour=hi), size=1) +
scale_fill_manual(values=c("gray51","white")) +
scale_colour_manual(values=c(NA,"red")) +
guides(colour=FALSE)
UPDATE: In response to your comment: I think a line plot makes it easier to see the trends and the relationships between each type. For example:
ggplot(data = z, aes(x=Time,y=val,colour=type)) +
geom_line() +
geom_point() +
geom_point(data=z[z$hi=="Y",], aes(x=Time, y=val), size=4, pch=1,
colour=hcl(195,100,40), stroke=1) +
scale_y_continuous(limits=c(0,max(z$val))) +
theme_bw()
Easy to do it with the legend, though you may want to be cautious about throwing users off with the abrupt change in color. Simply add an additional category to your x variable to indicate where you want the highlighting.
x<- xHigh <- c(rep(c("Type1", "Type2"),4))
xHigh[length(xHigh)] <- "Type2_highlight"
myHighlight <- rep("No",length(x))
myHighlight[length(myHighlight)] <- "Yes"
y<-c(4,5,6,7,3,4,5,2)
time<-c(2010,2010,2011,2011,2012,2012,2013,2013)
z<-data.frame(type = x, xHigh = xHigh, val=y, Time = time, myHighlight = myHighlight)
ggplot(data = z, aes(x=Time,y=val)) +
geom_bar(stat = "identity", position = "dodge", aes(fill=xHigh))+
scale_fill_manual(values = c(Type1 = "white", Type2 = "gray51", Type2_highlight = "red"))
Another potential option for highlighting a particular bar is to draw a box around it, like so:
ggplot(data = z, aes(x=Time,y=val)) +
geom_bar(stat = "identity", position = "dodge", aes(fill=type))+
scale_fill_manual(values = c(Type1 = "white", Type2 = "gray51")) +
geom_bar(aes(linetype = xHigh)
, fill = NA
, stat = "identity", position = "dodge"
, col = "red"
, show.legend = FALSE) +
scale_linetype_manual(values = c(Type1 = 0
, Type2 = 0
, Type2_highlight = 1))
Hope that helps.