This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 3 years ago.
I have the following dataset:
a<-data.frame(time=c("before","after","before","after"),
company=c(1,1,2,2),
value=c(3.751522,4.776224,3.838707,2.644144 ))
And to plot a graph I used the following code
a$company<-as.factor(a$company)
ggplot(a, aes(x=company, y=value, fill=time)) + geom_col(position="dodge")
As a result, I have the figure for two companies with after and before bars. However, how to fix a problem that the first bar of the company corresponds to the before and next to the after, and not vica versa as in my graph?
Does this achieve what you want:
a<-data.frame(time=factor(c("before","after","before","after"),
levels = c("before", "after")),
company=c(1,1,2,2),
value=c(3.751522,4.776224,3.838707,2.644144 ))
a$company<-as.factor(a$company)
ggplot(a, aes(x=company, y=value, fill=time)) + geom_col(position="dodge")
Related
This question already has answers here:
Show frequencies along with barplot in ggplot2
(5 answers)
Closed 8 years ago.
I want to plot frequency distribution of an [r] factor variable as a bargraph, where bars represent the frequency counts of the factor levels. I use ggplot2 to do that and there's no problem with that.
What I can't figure out is how to add frequency count labels to the bars in the bargraph. The syntax that I've tried is as follows:
ggplot(data, aes(x = factorvar)) + geom_bar(fill = "somecolor") + geom_text(aes(y = ???))
I think I thoroughly searched in stackoverflow and "R Graphics Cookbook" by W.Chang but I couldn't find any specific answer to what parameter should I match to "y" in the aesthetics of geom_text() above. I tried some variants like: (y = ..count..) but it didn't work.
I would appreciate any help. Thanks...
ggplot(data=diamonds, aes(x=clarity)) +
geom_bar() +
geom_text(stat='count', aes(label=..count..), vjust=-1)
This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 3 years ago.
I am attempting to build a chart for some LDA scores I have generated from bacterial abundances.
Here an example of the data:
Taxa <- c('Bacilli', 'Firmicutes', 'Slackia', 'Clostridium')
Level <- c('Class', 'Phylum', 'Genus', 'Genus')
Status <- c('Patient', 'Patient', 'Control', 'Control')
LDA.score <- c(3.5,2.0,-1,-3)
Example <- data.frame(Taxa, Level, Status, LDA.score)
I use this code to make the chart:
ggplot(data=Example, aes(x=Taxa, y=LDA.score, fill=Status)) + geom_bar(stat="identity", position="identity") + coord_flip()
I'd like the bars to be in numerical order so that the bars are grouped into control and patient. However, the resulting bar chart is in alphabetical order according to the x axis.
I have attempted to use reorder() but this doesn't seem to work.
Any help would be appreciated.
We could convert the 'Taxa' to factor based on the order of 'LDA.score' and then use that in ggplot
library(dplyr)
library(ggplot2)
Example %>%
mutate(Taxa = factor(Taxa, levels = as.character(Taxa)[order(LDA.score)])) %>%
ggplot(., aes(x=Taxa, y=LDA.score, fill=Status)) +
geom_bar(stat="identity", position="identity") +
coord_flip()
-output
This question already has answers here:
Show frequencies along with barplot in ggplot2
(5 answers)
Closed 8 years ago.
I want to plot frequency distribution of an [r] factor variable as a bargraph, where bars represent the frequency counts of the factor levels. I use ggplot2 to do that and there's no problem with that.
What I can't figure out is how to add frequency count labels to the bars in the bargraph. The syntax that I've tried is as follows:
ggplot(data, aes(x = factorvar)) + geom_bar(fill = "somecolor") + geom_text(aes(y = ???))
I think I thoroughly searched in stackoverflow and "R Graphics Cookbook" by W.Chang but I couldn't find any specific answer to what parameter should I match to "y" in the aesthetics of geom_text() above. I tried some variants like: (y = ..count..) but it didn't work.
I would appreciate any help. Thanks...
ggplot(data=diamonds, aes(x=clarity)) +
geom_bar() +
geom_text(stat='count', aes(label=..count..), vjust=-1)
This question already has answers here:
Show frequencies along with barplot in ggplot2
(5 answers)
Closed 8 years ago.
I want to plot frequency distribution of an [r] factor variable as a bargraph, where bars represent the frequency counts of the factor levels. I use ggplot2 to do that and there's no problem with that.
What I can't figure out is how to add frequency count labels to the bars in the bargraph. The syntax that I've tried is as follows:
ggplot(data, aes(x = factorvar)) + geom_bar(fill = "somecolor") + geom_text(aes(y = ???))
I think I thoroughly searched in stackoverflow and "R Graphics Cookbook" by W.Chang but I couldn't find any specific answer to what parameter should I match to "y" in the aesthetics of geom_text() above. I tried some variants like: (y = ..count..) but it didn't work.
I would appreciate any help. Thanks...
ggplot(data=diamonds, aes(x=clarity)) +
geom_bar() +
geom_text(stat='count', aes(label=..count..), vjust=-1)
This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 9 years ago.
I am plotting a barplot in ggplot:
ggplot(fastqc.dat,aes(y=fastqc.dat$ReadCount,x=fastqc.dat$Sample)) + geom_bar(stat="identity",position="identity",fill="darkblue") + xlab("Samples") + ylab("Read Counts") + opts(axis.text.x=theme_text(angle=-90))
My file 'fastqc.dat' looks like this:
Sample ReadCount
201304950-01_ATTCAGAA_R1 27584682
201304951-01_GAATTCGT_R1 25792086
201304952-01_CTGAAGCT_R1 36000000
201304953-01_GAGATTCC_R1 35634177
201304954-01_ATTACTCG_R1 88906701
It produces the following plot:
But I want to reorder the bars based on the read counts i.e. the Y axis. I tried a lot of things but it just won't happen. I even tried sorting fastqc.dat based on ReadCount column. Any suggestions?
... so bringing the helpful suggestions together, one solution would be:
fastqc.dat$Sample <- factor(fastqc.dat$Sample,
levels=fastqc.dat$Sample[order(fastqc.dat$ReadCount)])
and than use your code...
HTH
I got it to work. I had to add aes(x=fastqc.dat$Sample) to geom_bar() as below:
fastqc.dat$Sample <-factor(fastqc.dat$Sample, levels=fastqc.dat[order(fastqc.dat$ReadCount), "Sample"])
ggplot(fastqc.dat,aes(x=fastqc.dat$Sample,y=fastqc.dat$ReadCount)) + geom_bar(aes(x=fastqc.dat$Sample),stat="identity",position="identity",fill="darkblue") + xlab("Samples") + ylab("Read Counts") + opts(axis.text.x=theme_text(angle=-90))
This arranges the bars on X axis.