I have a dataframe through which I plot a bar plot through ggplot2 in R.
library(dplyr)
library(ggplot2)
library(reshape2)
Dataset<- c("MO", "IP", "MP","CC")
GPP <- c(1, 3, 4,3)
NPP<-c(4,3,5,2)
df <- data.frame(Dataset,GPP,NPP)
df.m<-melt(df)
ggplot(df.m, aes(Dataset, value, fill = variable)) +
geom_bar(stat="identity", position = "dodge")
my_se <- df.m %>%
group_by(Dataset) %>%
summarise(n=n(),
sd=sd(value),
se=sd/sqrt(n))
df.m %>%
left_join(my_se) %>%
ggplot(aes(x = Dataset, y = value, fill = variable)) +
geom_bar(stat="identity", position = "dodge")+
geom_errorbar(aes(x=Dataset, ymin=value-se, ymax=value+se), width=0.4, position = position_dodge(.9))+
scale_fill_manual(labels = c("GPP", "NPP"),values=cbp1)+
theme(legend.text=element_text(size=11),axis.text.y=element_text(size=11.5),
axis.text.x=element_text(size=11.5),axis.title.x = element_text(size = 12), axis.title.y = element_text(size = 12))+
theme_bw()+theme(legend.title =element_blank())+
labs(y= fn, x = "")
When my bargraph if plotted, the order of the bars is
I would like to rearrange the bars in order : MO, IP, MP, CC (not alphabetically).
Help would be appreciated.
You need to set your factor levels explicitly or R will pick an order for them.
In the case of characters R will pick alphabetical order. Since you want a non-alphabetical order you'll need to set levels inside of factor at some point before plotting (there are several places where you could do it).
df <- data.frame(Dataset = factor(Dataset, levels=c("MO", "IP"," MP", "CC")) ,GPP,NPP)
Try this (I changed the colors because cbp1 is not present):
df.m %>%
left_join(my_se) %>%
ggplot(aes(x = factor(Dataset,levels=c('MO', 'IP', 'MP', 'CC')), y = value, fill = variable)) +
geom_bar(stat="identity", position = "dodge")+
geom_errorbar(aes(x=Dataset, ymin=value-se, ymax=value+se), width=0.4, position = position_dodge(.9))+
scale_fill_manual(labels = c("GPP", "NPP"),values=c('pink','cyan'))+
theme(legend.text=element_text(size=11),axis.text.y=element_text(size=11.5),
axis.text.x=element_text(size=11.5),axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12))+
theme_bw()+theme(legend.title =element_blank())+
labs(y= "fn", x = "")
Related
I have a swimlane plot which I want to order by a group variable. I was also wondering if it is possible to label the groups on the ggplot.
Here is the code to create the data set and plot the data
dataset <- data.frame(subject = c("1002", "1002", "1002", "1002", "10034","10034","10034","10034","10054","10054","10054","1003","1003","1003","1003"),
exdose = c(5,10,20,5,5,10,20,20,5,10,20,5,20,10,5),
p= c(1,2,3,4,1,2,3,4,1,2,3,1,2,3,4),
diff = c(3,3,9,7,3,3,4,5,3,5,6,3,5,6,7),
group =c("grp1","grp1","grp1","grp1","grp2","grp2","grp2","grp2","grp1","grp1","grp1","grp2","grp2","grp2","grp2")
)
ggplot(dataset, aes(x = diff + 1, y = subject, group = p)) +
geom_col(aes(fill = as.factor(exdose)), position = position_stack(reverse = TRUE))
I want the y axis order by group and I want a label on the side to label the groups if possible
you can see from the plot it is ordered by subject number but I want it ordered by group and some indicator of group.
I tried reorder but I was unsuccessful in getting the desired plot.
As Stefan points out, facets are probably the way to go here, but you can use them with subtle theme tweaks to make it look as though you have just added a grouping variable on the y axis:
library(tidyverse)
dataset %>%
mutate(group = factor(group),
subject = reorder(subject, as.numeric(group)),
exdose = factor(exdose)) %>%
ggplot(aes(x = diff + 1, y = subject, group = p)) +
geom_col(aes(fill = exdose), color = "gray50",
position = position_stack(reverse = TRUE)) +
scale_y_discrete(expand = c(0.1, 0.4)) +
scale_fill_brewer(palette = "Set2") +
facet_grid(group ~ ., scales = "free_y", switch = "y") +
theme_minimal(base_size = 16) +
theme(strip.background = element_rect(color = "gray"),
strip.text = element_text(face = 2),
panel.spacing.y = unit(0, "mm"),
panel.background = element_rect(fill = "#f9f8f6", color = NA))
Here's some dummy data
dummy <- data.frame(numbers = 1:5,
symptomdate = as.Date(c("2012-08-30", "2012-08-30", "2012-08-31", "2012-09-01", "2012-09-01")),
reporteddate = as.Date(c("2012-09-02", "2012-09-03", "2012-09-05", "2012-09-07", "2012-09-08")),
dateofdeath = as.Date(c("2012-09-10", NA, NA, NA, "2012-09-31")),
gender = c("Female", "Male", "Male","Female", "Male"),
position = c("Resident", "Staff", "Resident", "Staff", "Staff"),
outbreakdate = as.Date(c("2012-08-31","2012-08-31","2012-08-31","2012-08-31","2012-08-31")))
each observation is a 'case'. I would like to create a histogram which shows the case count on the y axis, and also have a secondary y-axis which shows the cumulative count of cases, but I can't figure out how to make it using 'sec.axis'. Do I need to add a cumulative count to my dataframe first?
What I have so far:
ggplot(dummy, aes(x= symptomdate, group = position, fill = position)) + stat_bin(colour = "black", binwidth = 0.5, alpha = 1, position = "identity") + theme_bw() +
xlab("Symptom date") + ylab("Number of cases") + scale_x_date(breaks= date_breaks("1 day"), labels = date_format("%b-%d")) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + theme(legend.position="top") + scale_fill_manual(values = my_colours)
I know this must be simple but I've looked at countless posts and can't figure it out. Thank you in advance!
Try this. With your dummy data you can create the variables for cases and cumulative counts. After computing the scaling factor, you can reshape to long and sketch the plot with the desired structure. Here the code, where tidyverse functions have been used over dummy dataframe:
library(tidyverse)
#Code
newdf <- dummy %>% group_by(symptomdate) %>%
summarise(Count=n()) %>% ungroup() %>%
mutate(Cum=cumsum(Count))
#Scaling factor
sf <- max(newdf$Count)
newdf$Cum <- newdf$Cum/sf
#plot
newdf %>%
pivot_longer(-symptomdate) %>%
ggplot(aes(x=symptomdate)) +
geom_bar( aes(y = value, fill = name, group = name),
stat="identity", position=position_dodge(),
color="black", alpha=.6) +
scale_fill_manual(values = c("blue", "red")) +
scale_y_continuous(name = "Cases",sec.axis = sec_axis(~.*sf, name="Cum Cases"))+
labs(fill='Variable')+
theme_bw()
Output:
How can I make a graphic bar using barplot() or ggplopt() of an excel archive that has 83 columns?
I need to plot every column that has a >0 value on ich raw. (ich column represents a gene function and I need to know how many functions there is on ich cluster).
Iwas trying this,but it didn't work:
ggplot(x, aes(x=Cluster, y=value, fill=variable)) +
geom_bar(stat="bin", position="dodge") +
theme_bw() +
ylab("Funções no cluster") +
xlab("Cluster") +
scale_fill_brewer(palette="Blues")
Link to the excel:
https://github.com/annabmarques/GenesCorazon/blob/master/AllclusPathwayEDIT.xlsx
What about a heatmap? A rough example:
library(dplyr)
library(tidyr)
library(ggplot2)
library(openxlsx)
data <- read.xlsx("AllclusPathwayEDIT.xlsx")
data <- data %>%
mutate(cluster_nr = row_number()) %>%
pivot_longer(cols = -c(Cluster, cluster_nr),
names_to = "observations",
values_to = "value") %>%
mutate(value = as.factor(value))
ggplot(data, aes(x = cluster_nr, y = observations, fill = value)) +
geom_tile() +
scale_fill_brewer(palette = "Blues")
Given the large number of observations consider breaking this up into multiple charts.
It's difficult to understand exactly what you're trying to do. Is this what you're trying to achieve?
#install.packages("readxl")
library(tidyverse)
library(readxl)
read_excel("AllclusPathwayEDIT.xlsx") %>%
pivot_longer(!Cluster, names_to = "gene_counts", values_to = "count") %>%
mutate(Cluster = as.factor(Cluster)) %>%
ggplot(aes(x = Cluster, y = count, fill = gene_counts)) +
geom_bar(position="stack", stat = "identity") +
theme(legend.position = "right",
legend.key.size = unit(0.4,"line"),
legend.text = element_text(size = 7),
legend.title = element_blank()) +
guides(fill = guide_legend(ncol = 1))
ggsave(filename = "example.pdf", height = 20, width = 35, units = "cm")
I would like to have a colorblind friendly paletter for a barplot in ggplot.
I plot the barplot through this code
library(tidyverse)
my_se <- df %>%
group_by(groups) %>%
summarise(n=n(),
sd=sd(mean),
se=sd/sqrt(n))
# Standard error
df %>%
left_join(my_se) %>%
mutate(zone = factor(zone,labels = c("x1","x2","x3","x4","x5","x6","x7","x8","x9","x10","x11"))) %>%
ggplot(aes(x=zone, y=mean, fill = groups)) +
geom_col(position = position_dodge()) +
geom_errorbar(aes(x=zone, ymin=mean-se, ymax=mean+se), width=0.4, position = position_dodge(.9)) +
ggtitle("using standard error")+scale_fill_discrete(labels = c("GC", "IP", "MR","CS"))+
labs(y= an, x = "Land Cover")+theme_bw()+theme(legend.title =element_blank())+
theme(legend.text=element_text(size=11),axis.text.y=element_text(size=11.5),
axis.text.x=element_text(size=11.5),axis.title.x = element_text(size = 12), axis.title.y = element_text(size = 12))
I have made a custom color palette
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73")
But when I add this snippet code:
scale_fill_manual(values=cbPalette)
to my ggplot code it gives me this message
Scale for 'fill' is already present. Adding another scale for 'fill', which
will replace the existing scale.
Although the colors are changed but my whole legend labels get changed with it. I want to keep my legend variables as "GC", "IP", "MR","CS".
How can I change the colors of the bars with not effecting the legend labels of variables?
Pointing out to great suggestion from #Dave2e you can try this (Data used from similar questions you posted):
df %>%
left_join(my_se) %>%
mutate(zone = factor(zone,labels = c("x1","x2","x3","x4","x5","x6","x7","x8","x9","x10","x11"))) %>%
ggplot(aes(x=zone, y=meangpp, fill = groups)) +
geom_col(position = position_dodge()) +
geom_errorbar(aes(x=zone, ymin=meangpp-se, ymax=meangpp+se), width=0.4, position = position_dodge(.9)) +
ggtitle("using standard error")+
scale_fill_manual(labels = c("GC", "IP", "MR","CS"),values=cbPalette)+
labs(y= 'an', x = "Land Cover")+theme_bw()+theme(legend.title =element_blank())+
theme(legend.text=element_text(size=11),axis.text.y=element_text(size=11.5),
axis.text.x=element_text(size=11.5),axis.title.x = element_text(size = 12), axis.title.y = element_text(size = 12))
So I am currently plotting data from a excel sheet using R. The problem I am having is in regards to the legend. Here is the Picture: https://i.stack.imgur.com/Key98.jpg As you can see in the legend, the values go as follows: PP1, PP10, PP15, PP3, PP30, PP5. I have been trying to make it go in numerical order as PP1, PP3, PP5, PP10,PP15, PP30. I am not sure how to fix this problem as I am very new to R coding. Any help would be greatly appreciated!! This is how i have my Excel sheet formated: https://i.stack.imgur.com/OfNaY.jpg Here is my Code:
library("dplyr")
install.packages("ggplot2")
library("ggplot2")
install.packages("tidyverse")
library("tidyverse")
install.packages('reshape')
library('reshape')
# import data
NPPdata <- read.csv("C:\\Users\\rrami\\Desktop\\R-Data\\NPPdata.csv", header = TRUE)
ggplot(NPPdata , aes(x = N_Gradient, y=Values, colour = Group))+
geom_errorbar(aes(ymin=Values-Stdvalue, ymax=Values+Stdvalue), lwd =1.2)+
geom_line(lwd=1.5)+
ggtitle("Year 1 MONO Phrag [Branch Prob 0.1]")+
theme(plot.title = element_text(hjust =0.5)) +
labs(x = "N-Gradient", y ="INV%")+
theme(axis.text.x = element_text(size = 14), axis.title.x = element_text(size = 16),
axis.text.y = element_text(size = 14), axis.title.y = element_text(size = 16))
I've made an example with "iris". As you can see, on the second figure 'scale_fill_discrete' is used to change the order of the labels
library (tidyverse)
data(iris)
figure_1 <- iris %>%
gather(key = floral_components, value = values, -Species) %>%
ggplot(aes(x = floral_components, y = values, fill = Species)) +
geom_bar(stat='identity') +
labs(x = "Floral Components",
y = "Values",
fill = "Species")
figure_2 <- iris %>%
gather(key = floral_components, value = values, -Species) %>%
ggplot(aes(x = floral_components, y = values, fill = Species)) +
geom_bar(stat='identity') +
labs(x = "Floral Components",
y = "Values",
fill = "Species") +
scale_fill_discrete(labels = c("versicolor", "virginica", "setosa"))