I've produced a bar graph, however I want the data to be switched.
I want the blue area (the live cells) on the bottom and the dead cells on the top.
I tried reorder, reordering it by making it a factor but it all didn't work.
Who can help me?
Code I have now:
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=Type)) +
geom_bar(aes(fill = Type), stat="identity", position="stack", color="black") +
scale_fill_manual(values=c("lightblue","grey")) +
theme_bw() + theme(legend.position="right", axis.title.x = element_blank())
Graph I have now:
library(forcats)
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=fct_shift(Type))) +
geom_bar(aes(fill = Type), stat="identity", position="stack", color="black") +
scale_fill_manual(values=c("lightblue","grey")) +
theme_bw() + theme(legend.position="right", axis.title.x = element_blank())
Have you tried position_stack(reverse = TRUE)?
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=Type)) +
geom_bar(aes(fill = Type), stat="identity", position = position_stack(reverse = TRUE)), color="black") +
scale_fill_manual(values=c("lightblue","grey")) +
theme_bw() + theme(legend.position="right", axis.title.x = element_blank())
`install.packages("forcats")
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill = forcats::fct_rev(Type))) +
geom_bar(stat="identity", position="stack", color="black") +
scale_fill_manual(values=c("lightblue","grey"))`
Forcats did the trick
Related
I have a stacked bar graph and ggplot has automatically generated a color legend that I want to remove. I have tried show.legend=FALSE, theme(legend.position="none"), and guides(colour=FALSE) and none of those solutions have removed the legend. I'll include the code below.
ggplot(unique_per_day, aes(fill=Entity.Name,y=prop, x=Entity.Type, width = org.count, label=Entity.Name), show.legend=FALSE) +
geom_bar(position="fill", stat="identity", colour= "black") +
facet_grid(~Entity.Type, scales="free_x", space="free_x" ) +
theme(legend.position="none", panel.spacing.x = unit(0, "npc")) +
guides(colour=FALSE) +
geom_text(size = 2.4, position = position_stack(vjust = 0.5)) +
theme_void()
It would be easier to troubleshoot if you provided an example dataset (e.g. dput(unique_per_day)), but my guess is that you need to remove the "fill" aesthetic, instead of the "color" aesthetic, e.g.
ggplot(unique_per_day, aes(fill=Entity.Name,y=prop, x=Entity.Type, width = org.count, label=Entity.Name)) +
geom_bar(position="fill", stat="identity", colour= "black", show.legend=FALSE) +
facet_grid(~Entity.Type, scales="free_x", space="free_x" ) +
guides(fill = FALSE) +
geom_text(size = 2.4, position = position_stack(vjust = 0.5)) +
theme_void() +
theme(legend.position="none", panel.spacing.x = unit(0, "npc"))
Edit
Here is an example using the "palmerpenguins" example dataset:
With the original code:
library(tidyverse)
library(palmerpenguins)
penguins %>%
na.omit() %>%
filter(island == "Dream") %>%
ggplot(aes(x = species, y = 1, fill = sex)) +
geom_bar(position = "fill", stat = "identity", colour = "black") +
facet_grid(~ island, scales = "free_x", space = "free_x") +
theme(legend.position = "none", panel.spacing.x = unit(0, "npc")) +
guides(colour = FALSE) +
theme_void()
With "theme_void" moved above "theme":
penguins %>%
na.omit() %>%
filter(island == "Dream") %>%
ggplot(aes(x = species, y = 1, fill = sex)) +
geom_bar(position = "fill", stat = "identity", colour = "black") +
facet_grid(~ island, scales = "free_x", space = "free_x") +
theme_void() +
theme(legend.position="none", panel.spacing.x = unit(0, "npc"))
I have a data frame df
Group Time_Period mean uci lci
1 A Before 4.712195 5.054539 4.369852
2 A After 5.881463 6.241784 5.521142
3 B Before 5.349754 5.872940 4.826567
4 B After 6.653595 7.246231 6.060959
I want to plot this to illustrate that there is no difference in the mean increase between groups. I tried the following :
ggplot(df, aes(x=Time_Period, y=mean, fill=Group)) +
geom_bar(stat="identity", position=position_dodge(width = 1), color="black") +
geom_point(position = position_dodge(width = 1))+
geom_line(aes(group=Group, color=Group), color=c("cyan4","firebrick","cyan4","firebrick"), size =1, position = position_dodge(width = 1)) +
geom_errorbar(aes(ymin=lci, ymax=uci), position=position_dodge(width = 1)) +
theme_bw() +
scale_y_continuous(limits=c(-0.2,8), breaks= seq(0,300,1), minor_breaks=seq(0,300,0.5)) +
theme(panel.grid.minor = element_line(colour="lightgrey", size=0.5)) +
theme(panel.grid.major = element_line(colour="grey", size=0.5)) +
labs(y="Sales", x="Time Period", fill="Category") +
theme(axis.text.x = element_text(face="bold", size=12)) +
theme(axis.text.y = element_text(face="bold", size=12)) +
theme(axis.title.x = element_text(face="bold", size=16)) +
theme(axis.title.y = element_text(face="bold", size=16)) +
theme(legend.text= element_text(face="bold", size=12)) +
theme(legend.title= element_text(face="bold", size=16))
which plots:
However my manager is concerned it is difficult to differentiate the two lines due to the overlap, so he told me to rearrange the columns so that x is Group and fill is Time Period.
I tried the following:
ggplot(df, aes(x=Group, y=mean, fill=Time_Period)) +
geom_bar(stat="identity", position=position_dodge(width = 1), color="black") +
geom_point(position = position_dodge(width = 1))+
geom_line(aes(group=Group), color="black", size =1, position = position_dodge(width = 1)) +
geom_errorbar(aes(ymin=lci, ymax=uci), position=position_dodge(width = 1)) +
theme_bw() +
scale_y_continuous(limits=c(-0.2,8), breaks= seq(0,300,1), minor_breaks=seq(0,300,0.5)) +
theme(panel.grid.minor = element_line(colour="lightgrey", size=0.5)) +
theme(panel.grid.major = element_line(colour="grey", size=0.5)) +
labs(y="Sales", x="Group", fill="Time Period") +
theme(axis.text.x = element_text(face="bold", size=12)) +
theme(axis.text.y = element_text(face="bold", size=12)) +
theme(axis.title.x = element_text(face="bold", size=16)) +
theme(axis.title.y = element_text(face="bold", size=16)) +
theme(legend.text= element_text(face="bold", size=12)) +
theme(legend.title= element_text(face="bold", size=16))
But I can't work out how to get the lines to plot correctly between the two bars instead of just vertically in the centre, even if I adjust the "width" argument for position_dodge:
Please could anyone advise me on how to fix the plot?
You're looking for position_dodge2(). There's a little about it on the ggplot2 dodge reference, and a little more in the actual code on Github. The relevant section below, with some emphasis added:
Dodging preserves the vertical position of an geom while adjusting the
horizontal position. position_dodge2 is a special case of position_dodge
for arranging box plots, which can have variable widths. position_dodge2
also works with bars and rectangles. But unlike position_dodge,
position_dodge2 works without a grouping variable in a layer.
So here's the code, with some of the theming removed.
library(tidyverse)
txt = "
Group Time_Period mean uci lci
1 A Before 4.712195 5.054539 4.369852
2 A After 5.881463 6.241784 5.521142
3 B Before 5.349754 5.872940 4.826567
4 B After 6.653595 7.246231 6.060959"
df <- read.table(text = txt, header = TRUE) %>%
mutate(Group = fct_relevel(Group, "A", "B")) %>%
mutate(Time_Period = fct_relevel(Time_Period, "Before", "After"))
ggplot(df, aes(x=Group, y=mean, fill=Time_Period)) +
geom_bar(stat="identity", position=position_dodge(width = 1), color="black") +
geom_point(position = position_dodge(width = 1))+
geom_line(aes(group=Group), color="black", size =1,
position = position_dodge2(width = 1)) +
geom_errorbar(aes(ymin=lci, ymax=uci), position=position_dodge(width = 1)) +
theme_bw() +
scale_y_continuous(limits=c(-0.2,8), breaks= seq(0,300,1), minor_breaks=seq(0,300,0.5)) +
labs(y="Sales", x="Group", fill="Time Period")
Created on 2019-11-21 by the reprex package (v0.3.0)
I'm sure this is simple but I can't figure it out.
I have the following chart:
library(data.table)
library(magrittr)
library(ggplot2)
cambodia <-
data.table(Period = c("Funan", "Chenla/Zhenla","Khmer Empire","Dark Ages of Cambodia"),
StartDate = c(-500,550,802,1431),
EndDate = c(550,802,1431,1863),
Color = c("lightblue","lightgreen","lightyellow","pink")) %>%
extract(order(-StartDate)) %>%
extract(, Period := factor(Period,levels = Period))
ggplot() +
geom_segment(data=cambodia, aes(x=StartDate, xend=EndDate, y=Period, yend=Period, color=Color),
linetype=1, size=2) +
scale_colour_brewer(palette = "Pastel1")+
xlab("Date")+
ylab("Ruler")+
theme_bw() +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank()) +
theme(aspect.ratio = .2) +
theme(legend.position="none")
But I would like the labels to be off the axis and on the page. Either to the left or on top of the middle of the line. E.g.
Most of the examples of geom_text give me gobbledeegook. I can't seem to apply them to the factor data I have here. Do you know how to do this?
Thank you
Having the labels on the end of the segments might distort the visual mapping of segment length and location to year-range. You could put the labels in the middle of the segments instead.
library(data.table)
library(magrittr)
library(ggplot2)
library(stringr)
cambodia <-
data.table(Period = c("Funan", "Chenla/Zhenla","Khmer Empire","Dark Ages of Cambodia"),
StartDate = c(-500,550,802,1431),
EndDate = c(550,802,1431,1863),
Color = c("lightblue","lightgreen","lightyellow","pink")) %>%
extract(order(-StartDate)) %>%
extract(, Period := factor(Period,levels = Period))
ggplot(cambodia, aes(x=StartDate, xend=EndDate, y=Period, colour=Period)) +
geom_segment(aes(xend=EndDate, yend=Period), linetype=1, size=2) +
geom_label(aes(label=str_wrap(Period,12), x=(StartDate + EndDate)/2), size=3) +
scale_colour_brewer(palette = "Set1") +
xlab("Date")+ ylab("Ruler")+
theme_bw() +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
aspect.ratio = .2,
legend.position="none",
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
Or what about going minimal:
ggplot(cambodia, aes(x=StartDate, y=1)) +
geom_rect(aes(xmin=StartDate, xmax=EndDate, ymin=0.97, ymax=1.03, fill=Period),
show.legend=FALSE, colour="white", size=0.5) +
geom_label(aes(label=str_wrap(Period,12), x=(StartDate + EndDate)/2), size=3.5) +
geom_text(aes(label=StartDate, y=0.96), size=3.5) +
geom_text(aes(label=ifelse(EndDate==max(EndDate), EndDate,""), x=EndDate, y=0.96), size=3.5) +
scale_colour_brewer(palette = "Set1") +
scale_y_continuous(limits=c(0.95,1.05)) +
theme_void()
ggplot() +
geom_segment(data=cambodia, aes(x=StartDate, xend=EndDate, y=Period, yend=Period, color=Color),
linetype=1, size=2) +
geom_label(data=cambodia, aes(x=StartDate, y=Period, label = Period),
nudge_x = c(-300, -200, -200, -100)) +
scale_colour_brewer(palette = "Pastel1")+
xlab("Date")+
ylab("")+
theme_bw() +
theme(legend.position="none") +
theme(aspect.ratio = .2) +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
axis.line.y = element_blank(), axis.text.y = element_blank(),
axis.ticks.y = element_blank())
You need to use element_blank() to remove the y axis elements and then use nudge_x argument in geom_label to offset the labels appropriately.
I am making a bar graph trying to change the colour of my bars using this code, but it does not seem to be working. What is the problem?
ggplot(hd.m, aes(provinces, value)) + geom_bar(aes(fill="#0072B2"), position = "dodge", stat="identity") + scale_fill_discrete(guide=FALSE) + xlab("Provinces and Territories") + ylab("Percentage(%)") + ggtitle("Heart Disease Prevelance across Canada in 2008-2009") + theme_bw() + theme(panel.border = element_blank()) + theme(
plot.title = element_text(size=20),
axis.title.x = element_text(size=14), axis.title.y = element_text(size=14)) + geom_hline(yintercept=4.7)
Take the fill out of the aes.
library(ggplot2)
df <- data.frame(x = c("AB","BC","MB"), y = c(3.5,3.9,4.6))
# You have:
ggplot(df, aes(x,y)) + geom_bar(aes(fill="blue"), stat="identity")
# Try:
ggplot(df, aes(x,y)) + geom_bar(fill="blue", stat="identity")
I have a data frame as follows:
variable=c("D","D","C","C","C","A","B","B","B","B")
value=c(80,100,70,68,65,45,33,31,36,32)
Count=as.integer(c(5,10,4,5,2,7,3,5,6,2))
mean=c(93.3,93.3,68.2,68.2,68.2,45,33.4,33.4,33.4,33.4)
df=data.frame(variable=variable,value=value,Count=Count,mean=mean)
I can make a nice plot (where the size of the square corresponds to the count of observations with that particular x-value and y-value), as shown below:
ggplot(df, aes(variable, value)) + geom_point(aes(size = Count), pch=15) + guides(fill=guide_legend(title="New")) + theme(legend.text=element_text(size=rel(2.3)), legend.title=element_text(size=rel(2.3), face="plain"), legend.position="right", axis.text = element_text(size=rel(2.3)), axis.title = element_text(size = rel(2.3))) + labs(x="Topic", y = "Percentage Grade")
However, I now want to superimpose a horizontal bar to each of the four topics, indicating the mean percentage grade. Those values are stored in df$mean. I cannot figure out how to accomplish this. I have tried using the geom_line() function with the horizontal line option... but this seems to plot vertical lines!
ggplot(df, aes(variable, value)) + geom_point(aes(size = Count), pch=15) + guides(fill=guide_legend(title="New")) + theme(legend.text=element_text(size=rel(2.3)), legend.title=element_text(size=rel(2.3), face="plain"), legend.position="right", axis.text = element_text(size=rel(2.3)), axis.title = element_text(size = rel(2.3))) + labs(x="Topic", y = "Percentage Grade") + geom_line(stat = "hline", yintercept = df$mean)
Thank you...
You can do this with geom_segment:
ggplot(df, aes(variable, value)) +
geom_point(aes(size = Count), pch=15) +
geom_segment(aes(x=variable, y=mean-.1,
xend=variable, yend=mean+.1),
color="red", size=I(40))