How to make bar plot using this data [duplicate] - r

This question already has answers here:
Order discrete x scale by frequency/value
(7 answers)
How do you specifically order ggplot2 x axis instead of alphabetical order? [duplicate]
(2 answers)
ggplot2, Ordering y axis
(1 answer)
R ggplot ordering bars within groups
(1 answer)
Closed 26 days ago.
I want to develop a bar plot using this data with the same of columns as shown in the data.
Valley
Female
Young
Yearling
Class1
Class2
Class3
Hushey
144
160
162
24
18
29
I tried this code, it produce the bar plot but with changed order of columns. developing bar plot using pivot_longer function, which produced graph but also changed the order of columns.
ibex %>%
pivot_longer(cols = -Valley) %>%
ggplot(aes(x=name,y=value)) +
geom_col() +
facet_wrap(~Valley) +
scale_y_continuous(expand = expansion(0)) +
theme_bw() +
labs(title = "", x="", y="Number of Ibex sighted")

Related

Creating ggplot scatterplot with two subsets [duplicate]

This question already has answers here:
Plot with multiple lines in different colors using ggplot2
(1 answer)
ggplot combining two plots from different data.frames
(3 answers)
Multiple plots with variable color in R ggplot
(1 answer)
Closed 6 months ago.
I have 2 dataframes, "hot", and "cold" that I want to plot on the same ggplot scatterplot, in 2 different colours.
I want the "temperature" variable on the x-axis and "depth" variable on the y-axis.
I then want both dataframes plotted on this graph, with "hot" shown by red points and "cold" shown by blue points.
I have already plotted them individually, but can't figure out how to combine them into one plot. Example of the code used for the individual plots:
ggplot(hot, aes(x=temp, y=depth)) +
geom_point(size=0.1) +
labs(title="Depth Profile of Temperature in the Warm Months",x="Temperature (degC)", y = "Depth (m)") +
scale_y_reverse()

How can I change the order of the stacked bar plot in R? [duplicate]

This question already has an answer here:
ggplot2 geom_bar - how to keep order of data.frame [duplicate]
(1 answer)
Closed 2 years ago.
I want to change the order of the stacked bar plot in R? I am sharing the example below and the green bar will be under the blue bar, how can I can this order? See the code below.
d1<-data.frame(Gene=c("DNA","DNA",
"RNA","RNA",
"XX","XX"),
Gender=c("M","F","M","F","M","F"),
p_value=c( 0.5, 0.1,
0.6,0.01,
0.07,0.02
))
p<-d1 %>%
ggplot(aes(x=forcats::fct_reorder(Gene,p_value), y=p_value, fill=Gender)) +
geom_col(color="black",position=position_dodge()) +
coord_flip() +
scale_fill_manual(values=c('#6495ED','#2E8B57'))+
labs(x="Gene", y="p-value")
Just convert d1$Gendered into a factor and specify the levels in the order you want them.
d1$Gender <- factor(d1$Gender, levels = c("M", "F"))
Then, run the code to create your plot.

Setting x-axis in ggplot to end at the last date data is available for [duplicate]

This question already has answers here:
Remove space between plotted data and the axes
(3 answers)
ggplot2: Add secondary x label (year below months)
(2 answers)
Multi-row x-axis labels in ggplot line chart
(3 answers)
Closed 3 years ago.
I have a line graph with the number of visitors staying at a campsite per night. My dataset ends at 10-28-2019, but my graph extends past that date which is unnecessary and provides false information. I want the graph to end at max(nightcounts$date) but am not sure where this should properly go. I tried adding it in line 1 as
aes(max(nightcounts$date), nightcounts$Freq
as an addition to the current aesthetic. Secondly, I am tryin got reformat the x- axis labeling to have the number day below each point, and then the month as a second line below thse points such as
27 28 29 30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
September___________October
1 base <- ggplot(data=nightcounts, aes(nightcounts$date, nightcounts$Freq)) #using ggplot with x = date, y = counts
2 base + geom_line(col="darkorchid4", aes(group = 1)) + #calling geom_line to make a line connecting data points
3 geom_point() + #calling geom_point to add points for each data point
4 labs(title = "Number of Visitors Camping per Night", #adding labels
5 x = "Date",
6 y = "Visitors") +
7 scale_x_date(labels = date_format("%m-%d-%Y"), breaks = date_breaks("week")) +
8 ggsave("Camping by Date.png") #save the image
image of my current plot

Ordering categories in ggplot bar graph [duplicate]

This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
ggplot2: reorder bars in barplot from highest to lowest [duplicate]
(1 answer)
Plot data in descending order as appears in data frame [duplicate]
(1 answer)
Closed 4 years ago.
I have this database, which is grouped by the variables Pres and Pres_ti.
Pres Pres_ti count perc
1 CARD IMP ASSE 13 0.26530612
2 CARD IMP IAMC 34 0.69387755
3 CARD IMP SEGPRIV 2 0.04081633
4 CARD PRO ASSE 10 0.25641026
5 CARD PRO IAMC 27 0.69230769
6 CARD PRO SEGPRIV 2 0.05128205
I used it to make a bar plot with ggplot,
ggplot(g2, aes(x = factor(Pres), y = perc*100, fill = Pres_ti,
fct_reorder(perc)) ) +
geom_bar(stat="identity", width = 0.7)+
coord_flip()
With that code i made a graph which shows, within each category of pres, the share of the different Pres_ti categories.
I want to order the graph in order to get in the first place the bar where Pres_ti ASSE category has the highest percentage .
For example, in my data, CARD IMP should be first since ASSE percentage is 0.26>0.25
Any idea of how can i solve this problem?
Thanks
Maybe something like this will do the job:
ggplot(g2, aes(x = reorder(factor(Pres), -perc*100), y = perc*100, fill = Pres_ti,
fct_reorder(perc)) ) +
geom_bar(stat="identity", width = 0.7)+
coord_flip()

Why X- Axis is sorted alphabetically in geom_line [duplicate]

This question already has answers here:
ggplot2: change order of display of a factor variable on an axis
(2 answers)
Avoid ggplot sorting the x-axis while plotting geom_bar()
(6 answers)
Closed 7 years ago.
here is an example of my data:
test = data.frame(month_year = c("MAR13","APR13","MAY13","JUN13"),
turnover = c(10,15,25,10))
Here is my plot:
ggplot(data = test,
aes(x = month_year,
y = turnover,
group = 1))+
geom_line()
Now my question is why is the X-axis sorted alphabetically and how can I prevent it, because as you can see it makes no sense.
Thank you!
Just use :
test = data.frame(month_year = factor(c("MAR13","APR13","MAY13","JUN13"),levels=c("MAR13","APR13","MAY13","JUN13")),
turnover = c(10,15,25,10))

Resources