R contour plots - r

I have a set of data in a gridded map and air pollutant concentrations for each grid.
x <- seq(1:12)
y <- seq(0.1,3,0.24)
set.seed(5)
z <- runif(length(x)*length(y),0,10)
data <- expand.grid(x,y)
data$z <- z
colnames(data)<-c("x","y","z")
p1<-ggplot(data)+
aes(x = x, y = y, z = z, fill = z) +
geom_tile() +
coord_equal() +
geom_contour(color = "white", alpha = 0.5) +
scale_fill_distiller(palette="Reds")+
theme_bw()
print(p1)
We can see the contour lines, but filling color not like smooth contour plot.
I've tried some example from website, they are working fine. I am just wondering, is this due to I have same lat for each raw and lon for each col? Any suggestion how can I fix it?

Related

Which is the equivalen to seaborn hue in ggplot?

I'm starting to program in R and I'm getting stuck in this plot.
This is the plot I'm traying to make:
I'm able to do it with this code:
x <- seq(0, 10,1 )
y = x**2
z= x**3
plot(x, y, type="o", col="blue",xlab='x',ylab="y = x2")
lines(x,z,col="green")
I need to do it ussing ggplot, since I have to add futher formating, but I'm not finding the way to do it, I'm loking for the equivalen of the "hue" function on seaborn.
sns.catplot(x="sex", y="survived", hue="class", kind="point", data=titanic);
To use ggplot2, it is better to prepare a data frame with all the values. Furthermore, it is recommended to work with "long-format" data frame. We can then map the color to class, which is y and z in your example.
x <- seq(0, 10,1 )
y <- x**2
z <- x**3
# Load the tidyverse package, which contains ggplot2 and tidyr
library(tidyverse)
# Create example data frame
dat <- data.frame(x, y, z)
# Conver to long format
dat2 <- dat %>% gather(class, value, -x)
# Plot the data
ggplot(dat2,
# Map x to x, y to value, and color to class
aes(x = x, y = value, color = class)) +
# Add point and line
geom_point() +
geom_line() +
# Map the color as y is blue and z is green
scale_color_manual(values = c("y" = "blue", "z" = "green")) +
# Adjust the format to mimic the base R plot
theme_classic() +
theme(panel.grid = element_blank())
One way would be creating two dataframes separately
library(ggplot2)
df1 <- data.frame(x, y)
df2 <- data.frame(x, z)
ggplot(df1, aes(x, y)) +
geom_line(color = "blue") +
geom_point(color = "blue", shape = 1, size = 2) +
geom_line(data = df2, aes(x, z), color = "green") +
ylab("y = x2")

Overlay plot and histogram in R with ggplot

I am trying to overlay a Plot and a Histogram in R, usign the ggplot2 package.
The Plot contains a set of curves (visualized as straight lines due to logarithmich axis) and a horizontal line.
I would like to plot on the same image an histogram showing the density distribution of the crossing ponts between the curves and the horizontal line. I can plot the histogram alone but not on the graph because the aes-length is not the same (the last intersection is at x = 800, while the x asis is much longer).
the code I wrote is:
baseplot +
geom_histogram(data = timesdf, aes(v)) + xlim(0,2000)
where v contains the intersections between the curves and the dashed line.
Any ideas?
edited: as suggested I wrote a little reproducible example:
library(ggplot2)
xvalues <- c(0:100)
yvalues1 <- xvalues^2-1000
yvalues2 <- xvalues^3-100
yvalues3 <- xvalues^4-10
yvalues4 <- xvalues^5-50
plotdf <- as.data.frame(xvalues)
plotdf$horiz <- 5
plotdf$vert1 <- yvalues1
plotdf$vert2 <- yvalues2
plotdf$vert3 <- yvalues3
plotdf$vert4 <- yvalues4
baseplot <- ggplot(data = plotdf, mapping = aes(x= xvalues, y= horiz))+
geom_line(linetype = "dashed", size = 1)+
geom_line(data = plotdf, mapping = aes(x= xvalues, y = vert1))+
geom_line(data = plotdf, mapping = aes(x= xvalues, y = vert2))+
geom_line(data = plotdf, mapping = aes(x= xvalues, y = vert3))+
geom_line(data = plotdf, mapping = aes(x= xvalues, y = vert4))+
coord_cartesian(xlim=c(0, 100), ylim=c(0, 1000))
baseplot
v<-c(ncol(plotdf)-1)
for(i in 1:ncol(plotdf)){
v[i] <- plotdf[max(which(plotdf[,i]<5)),1]
}
v <- as.integer(v[-1])
timesdf <- as.data.frame(v)
# my wish: visualize baseplot and histplot on the same image
histplot <- ggplot() + geom_histogram(data = timesdf, aes(v)) +
coord_cartesian(xlim=c(0, 100), ylim=c(0, 10))

triangular plot using ggtern

I am trying to triplot some points with some regions in triangular plot this code:
library(ggtern)
g <- data.frame(x=c(1,.6,.6), y=c(0,.4,0), z=c(0,0,.4), Series="Green")
r <- data.frame(x=c(0,0.4,0), y=c(0,0,0.4), z=c(1,0.6,0.6), Series="Red")
p <- data.frame(x=c(0,0.4,0), y=c(1,0.6,0.6), z=c(0,0,0.4), Series="Purple")
DATA = rbind(g,r,p)
plot <- ggtern(data=DATA,aes(x,y,z)) +
geom_polygon(aes(fill=Series),alpha=.5,color="black",size=0.25) +
scale_fill_manual(values=as.character(unique(DATA$Series))) +
theme(legend.position=c(0,1),legend.justification=c(0,1)) +
labs(fill="Region",title="Sample Filled Regions")
print(plot)
I want to add some points to this plot that are taken from text file and I am reading their x,y and z coordinates. How I can these points to the plot?
if I try something like this it delete the previous plot:
plot <- ggtern(data = data.frame(x = cordnate_x, y = cordnate_y, z = cordnate_z),aes(x, y, z)) + geom_point() +theme_rgbg()
print(plot)
This is the plot that I need to add points to it
traingular plot
You can add points as if you have normal ggplot object.
g <- data.frame(x=c(1,.6,.6), y=c(0,.4,0), z=c(0,0,.4), Series="Green")
r <- data.frame(x=c(0,0.4,0), y=c(0,0,0.4), z=c(1,0.6,0.6), Series="Red")
p <- data.frame(x=c(0,0.4,0), y=c(1,0.6,0.6), z=c(0,0,0.4), Series="Purple")
DATA = rbind(g,r,p)
temp <- data.frame(x=c(0.4), y=c(0.6), z=c(0.4))
plot<- ggtern(data=DATA,aes(x,y,z)) +
geom_polygon(aes(fill=Series),alpha=.5,color="black",size=0.25) +
scale_fill_manual(values=as.character(unique(DATA$Series))) +
theme(legend.position=c(0,1),legend.justification=c(0,1)) +
labs(fill="Region",title="Sample Filled Regions") +
geom_point(data = temp, colour = "red") +
annotate("text", x = 0.3, y = 0.6, z = 0.4, label = "Some text")

Align x axes of box plot and line plot using ggplot

Im trying to align the x-axes of a bar plot and line plot in one window frame using ggplot. Here is the fake data I'm trying to do it with.
library(ggplot2)
library(gridExtra)
m <- as.data.frame(matrix(0, ncol = 2, nrow = 27))
colnames(m) <- c("x", "y")
for( i in 1:nrow(m))
{
m$x[i] <- i
m$y[i] <- ((i*2) + 3)
}
My_plot <- (ggplot(data = m, aes(x = x, y = y)) + theme_bw())
Line_plot <- My_plot + geom_line()
Bar_plot <- My_plot + geom_bar(stat = "identity")
grid.arrange(Line_plot, Bar_plot)
Thank you for your help.
#eipi10 answers this particular case, but in general you also need to equalize the plot widths. If, for example, the y labels on one of the plots take up more space than on the other, even if you use the same axis on each plot, they will not line up when passed to grid.arrange:
axis <- scale_x_continuous(limits=range(m$x))
Line_plot <- ggplot(data = m, aes(x = x, y = y)) + theme_bw() + axis + geom_line()
m2 <- within(m, y <- y * 1e7)
Bar_plot <- ggplot(data = m2, aes(x = x, y = y)) + theme_bw() + axis + geom_bar(stat = "identity")
grid.arrange(Line_plot, Bar_plot)
In this case, you have to equalize the plot widths:
Line_plot <- ggplot_gtable(ggplot_build(Line_plot))
Bar_plot <- ggplot_gtable(ggplot_build(Bar_plot))
Bar_plot$widths <-Line_plot$widths
grid.arrange(Line_plot, Bar_plot)
The gridlines on the x axes will be aligned if you use scale_x_continuous to force ggplot to use limits you specify.
My_plot <- ggplot(data = m, aes(x = x, y = y)) + theme_bw() +
scale_x_continuous(limits=range(m$x))
Now, when you add the layers, the axes will share the common scaling.

R ggplot: overlaying a kernel density chart on the y axis of a plot

I have a ggplot chart
q3 <- ggplot(y, aes(T,C))+ geom_line()
and the kernel density
den <-density(y$C)
How can I overlay a kernel density chart on the y axis?
Thanks for your help
Following up on DWin's idea, this might be the sort of thing you're looking for:
dat <- data.frame(x = 1:100,
y = 1:100,
z = rnorm(100))
ggplot(dat) +
geom_point(aes(x = y/100,y = x/100)) +
geom_density(aes(x = z)) +
coord_flip()
Note the reversal of the x and y aesthetics in geom_point.

Resources