How to plot bar plot in R? [duplicate] - r

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")

Related

Combining two different types of plots into one window using ggplot [duplicate]

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)

Plotting 2 scatterplots with ggplot [duplicate]

This question already has answers here:
Side-by-side plots with ggplot2
(14 answers)
Closed 2 years ago.
I want to plot 2 scatterplots on top of one another with ggplot but I am not very familiar with it. I have been trying to follow other examples but the layered approach to this package confuses me.
In bothfrontier_data I want the first column to be the x variable with respect to the 3rd column and the second column to be the x variable with respect to the 4th column. Also how can I add custom axis titles to this plot and add custom axis ranges?
Thank you
############# GGPLOT TO SHOW BOTH PLOTS SUPERIMPOSED ###################################
bothfrontier_data <- data.frame(std_portfolios_Qts, std_portfolios_Qsi,
All_Portfolio_Returns_Qts, All_Portfolio_Returns_Qsi)
head(bothfrontier_data)
# std_portfolios_Qts std_portfolios_Qsi All_Portfolio_Returns_Qts All_Portfolio_Returns_Qsi
#1 0.8273063 0.8194767 0.3421454 0.3357710
#2 0.8272188 0.8196555 0.3421551 0.3357853
#3 0.8273064 0.8192980 0.3421648 0.3357996
#4 0.8271314 0.8194769 0.3421744 0.3358139
#5 0.8272191 0.8194770 0.3421840 0.3358281
#6 0.8272193 0.8194772 0.3421935 0.3358423
dim(bothfrontier_data)
#[1] 501 4
BothFrontiers <- ggplot(bothfrontier_data, aes(x=std_portfolios_Qts)) +
geom_point(aes(y=All_Portfolio_Returns_Qts), color = "blue") +
geom_point(aes(y=All_Portfolio_Returns_Qsi), color = "red")
plot(BothFrontiers)
You can try:
library(ggplot2)
library(patchwork)
#Plot 1
g1 <- ggplot(bothfrontier_data,aes(x=std_portfolios_Qts,y=All_Portfolio_Returns_Qts))+geom_point(color='blue')+
ggtitle('Plot 1')
#Plot 2
g2 <- ggplot(bothfrontier_data,aes(x=std_portfolios_Qsi,y=All_Portfolio_Returns_Qsi))+geom_point(color='red')+
ggtitle('Plot 2')
#Final plot
g1/g2
You can modify axis with scale_x_continuous() and scale_y_continuous(). Labels can be added with xlab() and ylab(). I hope this can help.

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()

same bar width in ggplot2? [duplicate]

This question already has answers here:
A way to always dodge a histogram? [duplicate]
(2 answers)
Closed 8 years ago.
In this example:
library(ggplot2)
dat <- data.frame(a=factor(c(1,1,1,2,2,2,3,3,3,4)), b=c("A","B","D","A","B","C","A","B","D",NA), c=c(1,4,3,5,5,1,2,2,8,6))
plot <- ggplot(dat,aes(fill=b,x=a,y=c))
plot + geom_bar(width=.7, position=position_dodge(width=.7), stat = "identity")
factor 4 is wider than the other bars. Is there a way to make them all the same width?
Ideally you should have data for every combination even if it is zero. That means, with 1 in data$a you should have data all the four(A,B,C,D) and so on... try modifying your data frame like this and plot. NA category was referred to as "other" here.
library(ggplot2)
dat <- data.frame(a=factor(c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)),
b=c("A","B","C","D","other","A","B","C","D","other","A","B","C","D","other","A","B","C","D","other"),
c=c(1,4,0,3,0,5,5,1,0,0,2,2,0,8,0,0,0,0,0,6))
plot <- ggplot(dat,aes(fill=b,x=a,y=c))
plot + geom_bar(width=.7, position=position_dodge(width=.7), stat = "identity")
View this dataframe you will know the difference. You will obviously have missing bars corresponding to your data, which dnt look good. But im afraid this might be the only solution.

histogram from two column data: number of entries and number in R [duplicate]

This question already has answers here:
Create a histogram for weighted values
(3 answers)
Closed 6 years ago.
This is the head of a data set containing 101302 observations. It is listing vehicle weight, and the number of registrations. I want to plot this as a histogram in R.
r mkg
3 1495
1 1447
1 1401
1 2405
1 2635
2 2515
I need to plot a histogram of the mkg variable, but I need to allow for the number of registrations. I'm not sure how to approach this. I'm sorry, I'm sure this is basic but I've looked all day for an answer and haven't found one that works.
Using ggplot2 package, you can try something like this:
library(ggplot2)
ggplot(df, aes(x = mkg)) + geom_histogram() + facet_wrap(~r)
It will make as many plots as there are unique values in column r.
If you want to plot all histograms on the same plot, you can try this:
library(ggplot2)
ggplot(df, aes(x = mkg, fill = r)) + geom_histogram()

Resources