Using hypothetical data I want to generate these three plots in one plot.
I wonder how I can do it. Is it possible to do it using ggplot2 or fGarch packages?
Here an approach with ggplot2
library("ggplot2")
x <- 0:100
y <- c(dnorm(x, mean=50, sd=10),
dlnorm(x, meanlog=3, sdlog=.7),
dlnorm(100-x, meanlog=3, sdlog=.7))
df <- data.frame(
x=x,
y=y,
type=rep(c("normal", "right skewed", "left skewed"), each=101)
)
ggplot(df, aes(x, y, color=type)) + geom_line()
Related
I am using ggplot2 to plot maps that have the same extent (i.e. same spatial coverage) but that show different features.
This is how it looks like:
library(raster)
library(reshape2)
library(ggplot2)
# make-up data
r <- raster(system.file("external/test.grd", package="raster"))
s <- stack(r, r**2, r**3, r**4, r**5)
names(s) <- paste0("Field ",seq(1,5))
# convert to data frame
rast.df <- as.data.frame(s, xy=T)
# melt
rast.melt <- melt(rast.df, id.vars = c('x','y'), variable.name="field")
# plot
ggplot() +
geom_raster(data=rast.melt , aes(x=x, y=y, fill=value)) +
facet_wrap(~field) +
scale_fill_continuous(na.value="transparent")
The resulting figure looks quite crappy because there's one single legend for all the maps. Therefore, the maps have no contrast at all.
How can I use individual legends for each facet in the graph above?
Here's an approach with ggarrange from the ggpubr package:
library(ggpubr)
ggarrange(plotlist = lapply(split(rast.melt, rast.melt$field),function(x){
ggplot() + geom_raster(data=x , aes(x=x, y=y, fill=value)) +
scale_fill_continuous(na.value="transparent") +
ggtitle(x$field[1])}))
I have a combined data set of 8826 Obs and 4 variables. My column names are tVec , yVec, tVec, yVec.
I need to plot the 2 yVec against the x axis as single tVec with legends. I tried the below but plots only one plot.
plotnew <- ggplot(data=combined, aes(x=tVec, y= yVec, colour='variable')) + geom_line()
Plot looks like this:
Any ides on this. Have tired many examples. Just not getting it right.
Thanks.
You need to format your input data.frame:
combined = data.frame(tVec=1:100,yVec=rnorm(100),tVec=101:200,yVec=rnorm(100))
df = rbind(data.frame(x=combined$tVec,y=combined$yVec,label="first"),
data.frame(x=combined$tVec.1,y=combined$yVec.1,label="second"))
library(ggplot2)
plotnew <- ggplot(data=df, aes(x, y, colour=label))+
geom_line()
Or
df = rbind(data.frame(x=combined$tVec,y=combined$yVec,label="first"),
data.frame(x=combined$tVec,y=combined$yVec.1,label="second"))
library(ggplot2)
plotnew <- ggplot(data=df, aes(x, y, colour=label))+
geom_line()
Hope that help
I am trying to create a chart for each ID (column 1) plotting foo and bar by dayt on each chart, and bar needs to be on an inverted axis...
my data has form
ID <- rep(6:10, times=5)
foo <-rnorm(n=25, mean=0, sd=1)
bar <-rnorm(n=25, mean=10, sd=1)
dayt <-rnorm(n=25, mean= 1, sd=1)
df <-data.frame(ID,dat,x,y)
I have no idea where to go from here except that I know ggplot2 allows multiple objects to be added to a chart easily...
I am trying something like this
require(ggplot2)
require(plyr)
require(gridExtra)
pl <- dlply(df, .(ID), function(dat) {
ggplot(data = dat, aes(x = dayt, y = foo)) + geom_line() +
geom_point() + xlab("x-label") + ylab("y-label") +
geom_smooth(method = "lm")
})
ml <- do.call(marrangeGrob, c(pl, list(nrow = 5, ncol = 1)))
ggsave("my_plots.pdf", ml, height = 8, width = 11, units = "in")
but cant figure out how to add the second data to each plot as well as invert the axis...
any help would be great!
thanks
zr
It sounds like you want to create a simple scatter plot, with multiple charts for each ID and a reversed Y axis.
If you want to create one plot with multiple charts for each ID, you can use ggplot's faceting functions (facet_grid or facet_wrap). You can reverse the Y axis with the scale_y_reverse() function.
Here's one way to go about it:
library(ggplot2) # Load the library
p <- ggplot(df, aes(x=x, y=y)) + # Tell ggplot what you're plotting
geom_point() + # Tell ggplot it's a scatter plot
facet_wrap(~ ID) + # Plot one chart for each ID
scale_y_reverse() # Reverse the axis
p # Display the chart
How can I add the sample ID (row number) as labels to each point in this LDA plot using ggplot2?
Thanks
Script:
require(MASS)
require(ggplot2)
data(iris)
irisLda <- lda(iris[,-5],iris[,5])
irisLda <- lda(Species~.,data=iris)
plot(irisLda)
irisProjection <- cbind(scale(as.matrix(iris[,-5]),scale=FALSE) %*% irisLda$scaling,iris[,5,drop=FALSE])
p <- ggplot(data=irisProjection,aes(x=LD1,y=LD2,col=Species))
p + geom_point()
You simply need to use geom_text:
irisProjection$row_num = 1:nrow(irisProjection)
p <- ggplot(data=irisProjection, aes(x=LD1,y=LD2,col=Species)) +
geom_point() + geom_text(aes(label = row_num))
print(p)
Maybe you need to play around a bit with hjust and vjust, which are part of geom_text. You also might want to have a look at the directlabels package for smart label placement.
I have a dataframe with two columns x and y that each contain values between 0 and 100 (the data are paired). I want to correlate them to each other using binned scatter plots. If I were to use a regular scatter plot, it would be easy to do:
geom_point(aes(x=x, y=y))
but I'd like to instead bin the points into N bins from 0 to 100, get the average value of x in each bin and the average value of y for the points in that bin, and show that as a scatter plot - so correlate the binned averages instead of the raw data points.
is there a clever/quick way to do this in ggplot2, using some combination of geom_smooth() and geom_point? Or does it have to be pre-computed manually and then plotted?
Yes, you can use stat_summary_bin.
set.seed(42)
x <- runif(1e4)
y <- x^2 + x + 4 * rnorm(1e4)
df <- data.frame(x=x, y=y)
library(ggplot2)
(ggplot(df, aes(x=x,y=y)) +
geom_point(alpha = 0.4) +
stat_summary_bin(fun.y='mean', bins=20,
color='orange', size=2, geom='point'))
I suggest geom_bin2d.
DF <- data.frame(x=1:100,y=1:100+rnorm(100))
library(ggplot2)
p <- ggplot(DF,aes(x=x,y=y)) + geom_bin2d()
print(p)