This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 2 years ago.
I am trying the following plotting.
I have this data set:
Pathway Value Col.Code
AKTSig 1 r
HRAS 2 r
Lbind 3 h
GPCRact 4 r
ACHsig 5 h
ACEest -2 r
MRNAspl -3 h
Notch -4 h
Delta -5 r
Sonic -6 r
I would like to plot a graph that has these columns with pathway along the x axis, value up the y axis and the columns coloured by the Col.Code column. I have tried geom_col() from ggplot2 but this always rearranges the columns into a random order i.e. not highest value to most negative. I have also tried geom_bar() but this creates counts for the pathways and doesn't plot what I have described above.
You can use this:
library(dplyr)
ggplot(data,aes(x=reorder(Pathway,-Value),y=Value,fill=Col.Code))+geom_bar(stat='identity')
One other approach is with fct_reorder from the forcats package:
library(forcats)
ggplot(data,aes(x=fct_reorder(Pathway,-Value),y=Value,fill=Col.Code)) +
geom_bar(stat='identity') +
labs(x = "Pathway")
Related
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.
I have the following dataframe and I am using ggplot to plot the ind vs values.
ggplot(data=stats,aes(x=ind,y=values,fill=ind))+geom_bar(stat="identity")+coord_flip()+scale_fill_brewer()
stats
values ind
1 238970950 testdb_i
2 130251496 testdb_b
3 314350612 testdb_s
4 234212341 testdb_m
5 222281421 testdb_e
6 183681071 testdb_if
7 491868567 testdb_l
8 372612463 testdb_p
The plot in y-axis is in the form of 0e+00, 1e+08, 2e+08 and so on but instead I need it in the form of 100M(hundred million), 200M(two hunderd million) etc marks. How can I get the desired axes in ggplot?
You may try
ggplot(data=stats,aes(x=ind,y=values,fill=ind))+
geom_bar(stat="identity")+
coord_flip()+
scale_fill_brewer()+
scale_y_continuous(labels=function(x) paste0(x/1e6,"M"))
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")
This question already has answers here:
Making a stacked bar plot for multiple variables - ggplot2 in R
(3 answers)
Closed 9 years ago.
I have data that has the following format:
revision added removed changed confirmed
1 20 0 0 0
2 18 3 8 10
3 12 8 14 10
4 6 5 11 8
5 0 1 7 11
Each row represents a revision of a document. The first column is the revision number, and the remaining columns represent elements added, removed, changed, and confirmed (ready) in the respective revision. (In reality, there are more rows and columns, this is just an example.) Each number represents the amount of recorded additions, removals, changes, and confirmations in each respective revision.
What I need is a stacked barplot that looks like somthing like this:
I would like to do this in ggplot2. The exact visual look is not important (fonts, colours, and placement of the legend) as long as I can tweak it later. At the moment, it's the general idea I'm looking for.
I've looked at several questions and answers, e.g.
How do I do a Barplot of already tabled data?,
Making a stacked bar plot for multiple variables - ggplot2 in R,
barplot with 3 variables (continous X and Y and third stacked variable), and
Stacked barplot, but they all seem to make assumptions that don't match my data. I've also experimented with something like this:
ggplot(data) + geom_bar(aes(x=revision, y=added), stat="identity", fill="white", colour="black") + geom_bar(aes(x=revision, y=removed), stat="identity", fill="red", colour="black")
But obviously this does not create a stacked barplot because it just drawns the second geom_bar over the first.
How can I make a stacked barplot of my data using ggplot2?
Try:
library(reshape2)
dat <- melt(data, id="revision")
ggplot(dat, aes(x=revision, y=value, fill=variable)) +
geom_bar(stat="identity")
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()