ggplot(G, aes(x = State, y = Score, fill = State)) +
geom_bar(stat = "identity", position = "identity", width = 0.5) +
scale_y_continuous(labels = scales::comma) +
coord_flip()
This the code I am using and I am trying to add a line at the score of 236 so how to do it and how to improve the chart in general and any edit or suggestion are always welcome.
Just use:
geom_hline(yintercept = 236)
Might be worth while to reorder your y axis and use fill = Score. It would make the plot look something like this:
df %>%
ggplot(aes(reorder(State, -Assault), Assault, fill = Assault)) +
geom_col(width = 0.75, aes(fill = Assault)) +
labs(x = "State") +
geom_hline(yintercept = 200, size = 1) +
coord_flip() +
theme_classic()
You can use geom_vline() to do this. Since you have so many bars you will want to apply the vline after your geom_bar() so that it will show up on top of your bars (rather than underneath where you can only barely see it).
ggplot(G, aes(x = State, y = Score, fill = State)) +
geom_bar(stat = "identity", position = "identity", width = 0.5) +
geom_hline(yintercept=236, color="#000000", linetype="solid") +
scale_y_continuous(labels = scales::comma) +
coord_flip()
All I've done there is add the third line to your example above. ggplot2 always confuses me with horizontal and vertical especially when you do things like coord_flip(). I think I've got it correct (even though it looks wrong it's because of the flip), but if I'm mistaken and the line comes out horizontal replace that third line above with this:
geom_vline(xintercept=236, color="#000000", linetype="solid") +
and note the only two changes, which are that vline becomes hline and xintercept becomes yintercept.
Related
My question is about how to show data (or value) labels in a stacked and grouped bar chart using ggplot. The chart is in the form of what has been resolved here stacked bars within grouped bar chart .
The code for producing the chart can be found in the first answer of the question in the above link. An example data set is also given in the question in the link. To show the value labels, I tried to extend that code with
+ geom_text(aes(label=value), position=position_dodge(width=0.9), vjust=-0.25)
but this does not work for me. I greatly appreciate any help on this.
You need to move data and aesthetics from geom_bar() up to ggplot() so that geom_text() can use it.
ggplot(data=test, aes(y = value, x = cat, fill = cond)) +
geom_bar(stat = "identity", position = "stack") +
theme_bw() +
facet_grid( ~ year) +
geom_text(aes(label = value), position = "stack")
Then you can play around with labels, e.g. omitting the zeros:
ggplot(data=test, aes(y = value, x = cat, fill = cond)) +
geom_bar(stat = "identity", position = "stack") +
theme_bw() +
facet_grid( ~ year) +
geom_text(aes(label = ifelse(value != 0, value, "")), position = "stack")
... and adjusting the position by vjust:
ggplot(data=test, aes(y = value, x = cat, fill = cond)) +
geom_bar(stat = "identity", position = "stack") +
theme_bw() +
facet_grid( ~ year) +
geom_text(aes(label = ifelse(value != 0, value, "")), position = "stack", vjust = -0.3)
Try this. Probably the trick is to use position_stack in geom_text.
library(tidyverse)
test <- expand.grid('cat' = LETTERS[1:5], 'cond'= c(F,T), 'year' = 2001:2005)
test$value <- floor((rnorm(nrow(test)))*100)
test$value[test$value < 0] <- 0
ggplot(test, aes(y = value, x = cat, fill = cond)) +
geom_bar(stat="identity", position='stack') +
geom_text(aes(label = ifelse(value > 0, value, "")), position = position_stack(), vjust=-0.25) +
theme_bw() +
facet_grid( ~ year)
Created on 2020-06-05 by the reprex package (v0.3.0)
I came across this question the other day and tried to re-create it for myself. ggplot, facet, piechart: placing text in the middle of pie chart slices
. My data is in a very similar format, but sadly the accepted answer did not help, hence why I am re posting.
I essentially want to create the accepted answer but with my own data, yet the issue I run into is that coord_polar does not support free scale. Using the first answer:
I tried it using the second version of the answer, with the ddplyr version, but I also do not get my desired output. Using the second answer:
Clearly none of these has the desired effect. I would prefer to create one as with size pie charts, but only showed four as an example, follows: .
This I did in excel, but with one legend, and no background grid.
Code
title<-c(1,1,2,2,3,3,4,4,5,5,6,6)
type<-c('A','B','A','B','A','B','A','B','A','B','A','B')
value<-c(0.25,0.75,0.3,0.7,0.4,0.6,0.5,0.5,0.1,0.9,0.15,0.85)
piec<-data.frame(title,type,value)
library(tidyverse)
p1<-ggplot(data = piec, aes(x = "", y = value, fill = type)) +
geom_bar(stat = "identity") +
geom_text(aes(label = value), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y")
#facet_grid(title ~ ., scales = "free")
p1
piec <- piec %>% group_by(title) %>% mutate(pos=cumsum(value)-0.5*value)
p2<-ggplot(data = piec) +
geom_bar(aes(x = "", y = value, fill = type), stat = "identity") +
geom_text(aes(x = "", y = pos, label = value)) +
coord_polar(theta = "y")
#facet_grid(Channel ~ ., scales = "free")
p2
You don't have to supply different y values for geom_text and geom_bar (use y = value for both of them). Next you have to specify position in geom_text. Finally, remove scales from facets.
library(ggplot2)
title<-c(1,1,2,2,3,3,4,4,5,5,6,6)
type<-c('A','B','A','B','A','B','A','B','A','B','A','B')
value<-c(0.25,0.75,0.3,0.7,0.4,0.6,0.5,0.5,0.1,0.9,0.15,0.85)
piec<-data.frame(title,type,value)
ggplot(piec, aes("", value, fill = type)) +
geom_bar(stat = "identity", color = "white", size = 1) +
geom_text(aes(label = paste0(value * 100, "%")),
position = position_stack(vjust = 0.5),
color = "white", size = 3) +
coord_polar(theta = "y") +
facet_wrap(~ title, ncol = 3) +
scale_fill_manual(values = c("#0048cc", "#cc8400")) +
theme_void()
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.
I have produced a volcano plot, however the underlying data has gaps, i.e. the histogram data looks like:
When I produce the volcano plot it looks a bit silly:
Is it possible to apply a smoother to the shaded area to iron out the ribbed feature; surely, it must already have a smoothness associated with it, otherwise the shadow would drop back to 0 each time?
Code:
ggplot(fly2[fly2$Region == "different",], aes(x = Probability)) +
stat_density(aes(ymax = ..density.., ymin = -..density..),
fill = "grey50", colour = "grey50",
geom = "ribbon", position = "identity") +
facet_grid(. ~ Algorithm) + xlim(0,0.3) +
coord_flip()
link to the dput file:
http://pastebin.com/ba95WEab
Use adjust in geom_density.
For example, when I use adjust = 1.6, this is what I get
ggplot(fly2[fly2$Region == "different",], aes(x = Probability)) +
stat_density(aes(ymax = ..density.., ymin = -..density..),
fill = "grey50", colour = "grey50",
geom = "ribbon", position = "identity",
adjust=1.6) +
facet_grid(. ~ Algorithm) + xlim(0,0.3) +
coord_flip()
I am using ggplot to plot some data. It works fine but I'd like to control the shape of the plotted area and to remove the grey background.
This is the code I'm using right now:
ggplot(data.melted, aes(x = Year, y = value, colour = variable)) +
geom_line() +
scale_x_continuous("Year") +
scale_y_continuous("Fraction of papers") +
scale_colour_discrete("Topics")
and this is the output it produces:
ggplot(data.melted, aes(x = Year, y = value, colour = variable)) +
geom_line() +
scale_x_continuous("Year") +
scale_y_continuous("Fraction of papers") +
scale_colour_discrete("Topics") + theme(panel.background = element_blank())
To specify dimensions when saving the plot. See ?ggsave for additional options.
ggsave(p1, file = "plot.png", width = 5, height = 5)