ordering bars in geom_bar position=fill [duplicate] - r

This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 8 years ago.
I tried to finde a solution for my problem but I could not. Probably, my problem is easy for some of you. However, I need support. I would be greatful for any help.
I have made a ggplot for two factors: HGU_type and cycle_complexity to show their proportions:
I used:
g2<-ggplot(t,aes(x=HGU_type,fill = cycle_complexity))+ geom_bar(position="fill")
g2
The graph, I get looks as follow:
I want to have increasing order of the bars on the x-axis...first bar with "nod", second with "shake", third with "retr"...
I tried everything and cannot find the solution.
I would be grateful for a hint

As #Japp pointed out, it's always best to include a minimal reproducible example with your question. I created this data set
#sample data
set.seed(18)
t<-data.frame(
HGU_type=sample(c("jerk","nod","pro","retr","shake","tilt","turn"), 50, replace=T, prob=sample(7)),
cycle_complexity=sample(c("multiple", "single"), 50, replace=T)
)
And a plot like your original one is created by
ggplot(t,aes(x=HGU_type,fill = cycle_complexity))+ geom_bar(position="fill")
In order to change the order in which the bars are drawn, you need to change the levels of the factor used for the x-axis. The reorder() function makes it easy to reorder factors based on different properties. Here we will re-order based on the proportion of "multiple" in each group
t$HGU2<-reorder(t$HGU_type, t$cycle_complexity,FUN=function(x) mean(as.numeric(x)))
Then we can plot with
ggplot(t,aes(x=HGU2,fill = cycle_complexity))+ geom_bar(position="fill")
to get

Related

How to address overplotting in GGally::ggpairs()? [duplicate]

This question already has answers here:
Change colors in ggpairs now that params is deprecated
(2 answers)
Closed 1 year ago.
I'm using the ggpairs() function from the GGally package, but I'm having an issue dealing with overplotting.
Is there a good way to address the overplotting? I've tried setting the alpha value but I couldn't find out how to pass it to ggpairs(). I also looked into using geom_hex but again, I couldn't see how to use it with ggpairs().
Here's a simple example:
# Create example data
df <- data.frame(x1=rnorm(1e4),
x2=rnorm(1e4),
x3=runif(1e4))
# Pairs plot
GGally::ggpairs(df)
To reduce overplotting of the points you may modify the size aesthetic in point based layers displayed in the lower triangular of the plot matrix:
GGally::ggpairs(df, lower=list(continuous=GGally::wrap("points", size = .01)))

R: ggplot2 plotting 1 combined boxplot instead of 2 plots for 2 categories

This is probably a simple problem with a simple fix, but I cannot seem to get it right for some reason. I am trying to plot a continuous variable "cnt" against a categorical variable "yr" with 2 categories (1 or 2). The data is roughly split in half into year 1 and year 2.
When I try the following code to compare the boxplots over 2 years, I get the following graph however:
ggplot(data=rawd,aes(x=yr,y=cnt, color=yr))+
geom_boxplot()+
labs(x='Year', y= 'Hourly Usage Count')
I am not sure why that is the case. When I try to visualize it in any other format (such as using jitter or geom_point) there is a clear split between yr=1 and yr= 2 (see below)
I am also facing an issue when using the simple boxplot command on R, where I get the following curve:
boxplot(rawd$yr, rawd$cnt)
If anyone can see what I am doing wrong, please let me know.
As mentioned in the comment changing the 'yr' column in question to type 'factor' seems to fix it

Settings y-axis limits when stat = "summary" (binomial data) [duplicate]

This question already has answers here:
geom_bar bars not displaying when specifying ylim
(4 answers)
Closed 8 months ago.
I am trying to create a barplot using ggplot2, with the y axis starting at a value greater than zero.
Lets say I have the means and standard errors for hypothetical dataset about carrot length at three different farms:
carrots<-NULL
carrots$Mean<-c(270,250,240)
carrots$SE<-c(3,4,5)
carrots$Farm<-c("Plains","Hill","Valley")
carrots<-data.frame(carrots)
I create a basic plot:
p<-ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_bar(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0)
p
This is nice, but as the scale runs from 0 to it is difficult to see the differences in length. Therefore, I would like to rescale the y axis to something like c(200,300). However, when I try to do this with:
p+scale_y_continuous('Length (mm)', limit=c(200,300))
The bars disappear, although the error bars remain.
My question is: is it possible to plot a barplot with this adjusted axis using ggplot2?
Thank you for any help or suggestions you can offer.
Try this
p + coord_cartesian(ylim=c(200,300))
Setting the limits on the coordinate system performs a visual zoom;
the data is unchanged, and we just view a small portion of the original plot.
If someone is trying to accomplish the same zoom effect for a flipped bar chart, the accepted answer won't work (even though the answer is perfect for the example in the question).
The solution for the flipped bar chart is using the argument ylim of the coord_flip function. I decided to post this answer because my bars were also "disappearing" as in the original question while I was trying to re-scale with other methods, but in my case the chart was a flipped one. This may probably help other people with the same issue.
This is the adapted code, based on the example of the question:
ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_col(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0) +
coord_flip(ylim=c(200,300))
Flipped chart example

text annotation to a graph in ggplot

I am drawing a PC plot using ggplots.
I know this question has been answered in some previous posts but I could not still solve my problem.
I have a data set called tab which is the output of PCA
sample.id pop EV1 EV2
HT185_MK8-2.sort.bam HA_27 -0.03796869 0.046369552
HT48_SD1A-37.sort.bam HA_14 0.04208393 0.032961404
HT53_IA1A-10.sort.bam HA_1 -0.02580365 0.005262476
HT260_MK1-4.sort.bam HA_20 -0.06090545 0.005578504
HT170_SD2W-14.sort.bam HA_17 0.01288395 0.012117833
Q093_MK7-13.sort.bam HA_26 0.06310162 0.188558067
I want to add labels on each dot in the plot, theses dots are individuals from several populations. So I want to give them their population ID (pop column in the data set).
I am using something this
ggplot(data=tab,aes(EV1,EV2, label=tab[,2])) + geom_point(aes(color=as.factor(pop))) + ylab("Principal component 2") + xlab("Principal component 1")
But I do not get my desired output.
This is my PC plot!
So could anyone help me to add population label on each dot in the plot!
Thanks
Try geom_text:
geom_text(aes(label=as.character(pop)),hjust=0,vjust=0)
Also consider looking into plotly, or setting a threshold on the labels, because labeling every point will lead to a very crowded plot, and probably very little additional useful information.

Adjust the scale per each individual facet/plot rather than per plots w/ shared axes?

I am using ggplot2 to make a graph that includes 15 plots (attached) and the following command:
ggplot(metrics, aes(Time.step, Value, color=Distance_f))+geom_line()+geom_point()+facet_grid(Metric~Distance_f, scales="free")+
scale_color_manual(values = c("red", "green", "blue"))+geom_smooth(method="lm")+theme_bw()
Given that the range of values in each variable is different I would like to be able to adjust the scale based on the values.
I've tried scales="free" and scales="free_y" which improve the display but they force the scale for plots along the axis to be shared.
What would be good way of making the scale different for each plot? (e.g. it's difficult to see the data on plot 1 on row 2).
Any advice would be much appreciated. Many thanks!!pls see image here:
you could use facet_wrap instead of facet_grid

Resources