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.
Related
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)
I am looking to scale the x axis on my barplot to time, so as to accurately represent when measurements were taken.
I have these data frames:
> Botcv
Date Average SE
1 2014-09-01 4.0 1.711307
2 2014-10-02 5.5 1.500000
> Botc1
Date Average SE
1 2014-10-15 2.125 0.7180703
2 2014-11-12 1.000 0.4629100
3 2014-12-11 0.500 0.2672612
> Botc2
Date Average SE
1 2014-10-15 3.375 1.3354708
2 2014-11-12 1.750 0.4531635
3 2014-12-11 0.625 0.1829813
I use this code to produce a grouped barplot:
covaverage <- c(Botcv$Average,NA,NA,NA)
c1average <- c(NA,NA, Botc1$Average)
c2average <- c(NA,NA, Botc2$Average)
date <- c(Botcv$Date, Botc1$Date)
averagematrix <- matrix(c(covaverage,c1average, c2average), nrow=3, ncol=5, byrow=TRUE)
barplot(averagematrix,date, xlab="Date", ylab="Average", axis.lty=1, space=NULL,width=3,beside=T, ylim=c(0.00,6.00))
R plots the bars equal distances apart by default and I have been trying to find a workaround for this. I have seen several other solutions that utilise ggplot2 but I am producing plots for my masters thesis and would like to keep the appearance of my barplots in line with other graphs that I have created using base R graphics. I also want to add error bars to the plot. If anyone could provide a solution then I would be very grateful!! Thanks!
Perhaps you can use this as a start. It is probably easier to use boxplots, as they can be put at a given x position by using the at argument. For base barplots this cannot be done, but you can use rectangle instead to replicate the barplot look. Error bars can be added using arrows or segments.
bar_w = 1 # width of bars
offset = c(-1,1) # offset to avoid overlapping
cols = grey.colors(2) # colors for different types
# combine into a single data frame
d = data.frame(rbind(Botc1, Botc2), 'type' = c(1,1,1,2,2,2))
# set up empty plot with sensible x and y lims
plot(as.Date(d$Date), d$Average, type='n', ylim=c(0,4))
# draw data of data frame 1 and 2
for (i in unique(d$type)){
dd = d[d$type==i, ]
x = as.Date(dd$Date)
y = dd$Average
# rectangles
rect(xleft=x-bar_w+offset[i], ybottom=0, xright=x+bar_w+offset[i], ytop=y, col=cols[i])
# errors bars
arrows(x0=x+offset[i], y0=y-0.5*dd$SE, x1=x+offset[i], y1=y+0.5*dd$SE, col=1, angle=90, code=3, length = 0.1)
}
If what you want to get is simply the theme that will match the base theme the + theme_bw() in ggplot2 will achieve this:
data(mtcars)
require(ggplot2)
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
theme_bw()
Result
Alternative
boxplot(mpg~cyl,data=mtcars)
If, as you said, the only thing you want to achieve is similar look, and you have working plot in the ggplot2 using the theme_bw() should produce plots that are indistinguishable from what would be derived via the standard plotting mechanism. If you feel so inclined you may tweak some minutiae details like font sizes, thickness of graph borders or visualisation of outliers.
This question already has answers here:
Is there an equivalent in ggplot to the varwidth option in plot?
(2 answers)
Closed 8 years ago.
I have a boxplot that I made in R with ggplot2 analagous to the sample boxplot below.
The problem is, for the values on the y axis (in this sample, the number of cylinders in the car) I have very different frequencies -- I may have included 2 8 cylinder cars, but 200 4 cylinder cars. Because of this, I'd like to be able to resize the boxplots (in this case, change the height along the y axis) so that the 4 cylinder boxplot is a larger portion of the chart than the 8 cylinder boxplot. Does someone know how to do this?
As #aosmith mentioned, varwidth is the argument you want. It looks like it may have been accidentally removed from ggplot2 at some point (https://github.com/hadley/ggplot2/blob/master/R/geom-boxplot.r). If you look at the commit title, it is adding back in the varwidth parmeter. I'm not sure if that ever made into the cran package, but you might want to check your version. It works with my version: ggplot2 v.1.0.0 I'm not sure how recently the feature was added.
Here is an example:
library(ggplot2)
set.seed(1234)
df <- data.frame(cond = factor( c(rep("A",200), rep("B",150), rep("C",200), rep("D",10)) ),
rating = c(rnorm(200),rnorm(150, mean=0.2), rnorm(200, mean=.8), rnorm(10, mean=0.6)))
head(df, 5)
tail(df, 5)
p <- ggplot(df, aes(x=cond, y=rating, fill=cond)) +
guides(fill=FALSE) + coord_flip()
p + geom_boxplot()
Gives:
p + geom_boxplot(varwidth=T)
Gives:
For a couple of more options, you can also use a violin plot with scaled widths (the scale="count" argument):
p+ geom_violin(scale="count")
Or combine violin and boxplots to maximize your information.
p+ geom_violin(scale="count") + geom_boxplot(fill="white", width=0.2, alpha=0.3)
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:
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()