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()
Related
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")
This question already has answers here:
ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"
(6 answers)
Closed 1 year ago.
I have the below table on data "data_temp" and I'm trying to plot a trend line using "geom_line", but line does not appear.
> head(data_temp,15)
Mês n
1 Janeiro 7432
2 Fevereiro 7077
3 Março 7494
4 Abril 7760
5 Maio 7755
6 Junho 7516
7 Julho 7320
8 Agosto 7084
9 Setembro 7134
10 Outubro 7390
11 Novembro 6973
12 Dezembro 8622
This is the code I used:
ggplot(data = data_temp, aes(x=Mês, y=n))+
geom_line() +
geom_point()
You can see below that the points work, but the line does not appear.
I tried so many codes in a different way, but I haven't suceed so far. I hope anyone can help me.
The x = "Mês", means "Month", but is in portuguese.
Many thanks,
Wil
Try
library(ggplot2)
ggplot(data = data_temp, aes(x=Mês, y=n, group=1))+
geom_line() +
geom_point()
to get
The geom_line-functions needs a group to "know" which dot's should be connected. Since you don't have a grouping variable in your data, you simply add group = 1 to group all dots into the same group.
This question already has answers here:
ggplot combining two plots from different data.frames
(3 answers)
Plot two graphs in same plot in R
(17 answers)
Closed 2 years ago.
Let's say that I have two data frames defined in such way :
df <- data.frame(x=rep(1:3, 3), val=sample(1:100, 9),
variable='category')
df1 <- data.frame(x=rep(4:6, 3), val=sample(1:100, 9),
variable='category')
And I want to plot them both on one graph in such way that for x from 1 to 3 it would be the line, and for x from 4 to 6 it would be dots. So
plot_1<-ggplot(data=df,aes(x=x,y=val))+geom_line(aes(colour=variable))
plot_2<-ggplot(data=df1,aes(x=x,y=val))+geom_point(aes(colour=variable))
plot_grid(plot_1,plot_2,nrow = 1,ncol=1)
And in output I get the graph following :
So instead of line from 1 to 3 and dots from 4 to 6 I have just the first graph (line from 1 till 3).
Is there some easy way how to solve this problem ?
If you want to plot the data on the same graph you can try :
library(ggplot2)
ggplot() + aes(x, val) +
geom_line(data = df) + geom_point(data = df1)
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
This question already has an answer here:
How to create grouped barplot with R [duplicate]
(1 answer)
Closed 3 years ago.
I have a data frame as follows:
reason_code num_stayed num_disconnected
1 60 2
2 113 3
3 212 2
4 451 6
.....
I basically want to plot the bar plot to compare for each reason_code, how many stayed and how many left? And I want to show these side by side.
That is in the same plot. Have two bars for each reason code. One bar in (says) red the other in green.
How do I plot them in R?
You can use the beside argument in barplot to accomplish this. Here's a very quick example:
example <- data.frame(reason_code=c(1,2,3,4),
num_stayed=c(60,113,212,451),
num_dx=c(2,3,2,6))
barplot(height=t(as.matrix(example[c("num_stayed","num_dx")])),beside=TRUE)
Note that I had to transpose it to get the barplot to interpret it correctly. See also this answer from Cross-Validated.
Here's a solution using ggplot:
require(ggplot2)
data = data.frame(reason_code = c(1,2,3,4),
num_stayed = c(60,113,212,451),
num_disconnected = c(2,3,2,6))
data = rbind(data.frame(type = "num_stayed", val = data$num_stayed, reason_code = data$reason_code),
data.frame(type = "num_disconnected", val = data$num_disconnected, reason_code = data$reason_code))
ggplot(data, aes(y=val, x=reason_code, fill=type)) + geom_bar(stat="identity", position="dodge")