R- Group jitter in factored boxplot? [duplicate] - r

This question already has answers here:
ggplot2 - jitter and position dodge together
(2 answers)
Closed 3 years ago.
Is it possible to group the jitter in a boxplot like mine so that the data points line up with the factor for each market? Right now it's lining up by market name. I colored them to show which ones should be grouped.
My code
p<-ggplot(droplevels(subset(sData,STORE_TYPE=='Test')),aes(factor(MARKET_NAME),DST_UNITS))
p +
geom_boxplot(aes(fill=factor(PROGRAM_STATUS,c("PRE-PROGRAM","POST-PROGRAM")), outlier.shape=NA) +
geom_jitter(aes(color=factor(PROGRAM_STATUS,c("PRE-PROGRAM","POST-PROGRAM"))),position=position_jitter(width=0))

Solution provided in comments by Didzis Elferts and link to this question
sData<-droplevels(subset(sData,STORE_TYPE=='Test'))
ggplot(sData,aes(x=factor(MARKET_NAME),y=DST_UNITS,fill=factor(PROGRAM_STATUS,c("PRE-PROGRAM","POST-PROGRAM")))) +
geom_boxplot(outlier.shape=NA) +
geom_point(position=position_jitterdodge())

Related

Multiple line on one Chart in R [duplicate]

This question already has answers here:
Plotting two variables as lines using ggplot2 on the same graph
(5 answers)
Closed 3 years ago.
I am trying to plot multiple line on one chart.
They all have the same X- axis in months, but different observation for y-axis.
I have tried writing this code but I keep on getting an error.
Can someone point me towards what I am doing wrong?
"Test3" is the same of my data set, "Oil_1" represents the first Y observation, "Oil_2" second observation and "Month" is the X-axis
ggplot(test3, + aes(x = Months)) +
geom_line(aes(y=oil_1),colour="blue")+
geom_line(aes(y=oil_2),colour="red") +
ylab(label="Production")+
xlab("Months")
You have an extra "+" before the first aesthetic call, that could be the problem.

How can I sort a bar graph from highest to lowest in R? (ggplot2) [duplicate]

This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 3 years ago.
How can I sort a bar graph from highest to lowest in R? (ggplot2)
the code is this, but feel free to do a better code haha
Ps: it is a huge data
ggplot(kiva, aes(repayment_interval, loan_amount, fill = repayment_interval)) +
geom_bar(stat = "identity") +
ggtitle("Total of loan for different types of repayment intervals")
I assume you want highest to lowest y-variable, loan_amount.
Difficult to answer without example data but something like this should work, using reorder. Also geom_col = less typing.
ggplot(kiva,
aes(reorder(repayment_interval, -loan_amount), loan_amount),
fill = repayment_interval)) +
geom_col() +
ggtitle("Total of loan for different types of repayment intervals")

Sorting DataFrame for ggplot barplot [duplicate]

This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 6 years ago.
I have a data frame df1 and want to draw a barplot of AccountExecutive and their corresponding ClearRate where the bars are arranged so that it is decreasing from left to right.
I tried this code but the resulting graph still reflects AccountExecutive order as it appears in df1
ggplot(arrange(df1, -ClearRate), aes(x = AccountExecutive, y = ClearRate)) +
geom_bar(stat="identity")
Can anyone help me correcting this code?
NOTE: Not a duplicate of the previous question because that one asks for an arbitrary positioning of the x axis labels. This question asks how to sort x-axis labels considering their y-axis values.
Try this one the code below should reorder AE according to clearance rate
ggplot(df1,aes(x=reorder(AccountExecutive,-ClearRate),y=ClearRate))+geom_bar(stat"identity")
here is the more about reorder function
Reorder bars in geom_bar ggplot2

How to show count of each bin on histogram on the plot [duplicate]

This question already has answers here:
How to put labels over geom_bar in R with ggplot2
(4 answers)
Closed 8 years ago.
I figured out how to get the count of each bin from ggplot, does anyone know how to show these numbers on the plot?
g <- ggplot()+geom_histogram()
ggplot_build(g)$data[[1]]$count
You can add a stat_bin to do the calculations of counts for you, and then use those values for the labels and heights. Here's an example
set.seed(15)
dd<-data.frame(x=rnorm(100,5,2))
ggplot(dd, aes(x=x))+ geom_histogram() +
stat_bin(aes(y=..count.., label=..count..), geom="text", vjust=-.5)

Order bars in ggplot [duplicate]

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.

Resources