ggplot2 - Vertical profile in r plot() -
i building vertical profile plot of water columns. issue dots connected on x observations, , not y observations. under ggplot, know geom_path can this, can't use ggplot want add several x axis. therefore using plot(). here tried:
storfjorden <- read.delim("c:/users/carvi/desktop/storfjorden.txt") smoof=smooth.spline(storfjorden$fluorescence,storfjorden$depth,spar=0.50) plot(storfjorden$fluorescence,storfjorden$depth,ylim=c(80,0),type="n") lines(smoof)
as see, dots connected through x observations. observe vertical profile, see them connected through y observations. tried ordering them depth (using order()) , didn't affect result. has clue?
if, alternative, have idea how plot different lines different axis on single plot (temperature, salinity, fluorescence), may use geom_path (). thank you!
**an emerging question have may answer, there way in ggplot make geom_smooth(), observations connected in order appear instead of x axis?
ggplot(melteddf,aes(y=depth,x=value))+geom_path()+facet_wrap +(~variable,nrow=1,scales="free_x")+scale_y_reverse() +geom_smooth(span=0.5,se=false)
i tried using smooth.spline, didn't recognize object in geom_path. thanks!
there reason ggplot2
makes difficult plot multiple x-axes on single plot -- leads difficult read (or worse, misleading) graphs. if have motivating example why example not fall 1 of categories, might allow more know more details. below, however, 2 workarounds might help.
here quick mwe address question -- might more helpful if gave looks actual data, @ least gets things on different scales (though, no structure, plots rather messy).
note using dplyr
several manipulations , reshape2
melt
data long format easier plotting.
library(dplyr) library(reshape2) df <- data.frame( depth = runif(20, 0, 100) %>% round %>% sort , measurea = rnorm(20, 10, 3) , measureb = rnorm(20, 50, 10) , measurec = rnorm(20, 1000, 30) ) melteddf <- df %>% melt(id.vars = "depth")
the first option use facets plot data next each other:
melteddf %>% ggplot(aes(y = depth , x = value)) + geom_path() + facet_wrap(~variable , nrow = 1 , scales = "free_x") + scale_y_reverse()
the second standardize data, plot that. here, using z-score, though if have reason use else (e.g. scaled center @ "appropriate" amount of whatever variable using) change formula:
melteddf %>% group_by(variable) %>% mutate(standardized = (value - mean(value)) / sd(value) ) %>% ggplot(aes(y = depth , x = standardized , col = variable)) + geom_path() + scale_y_reverse()
if need plot multiple sites, here sample data sites:
df <- data.frame( depth = runif(60, 0, 100) %>% round %>% sort , measurea = rnorm(60, 10, 3) , measureb = rnorm(60, 50, 10) , measurec = rnorm(60, 1000, 30) , site = sample(letters[1:3], 60, true) ) melteddf <- df %>% melt(id.vars = c("site", "depth"))
you can either use facet_grid
(my preference):
melteddf %>% ggplot(aes(y = depth , x = value)) + geom_path() + facet_grid(site~variable , scales = "free_x") + scale_y_reverse()
or add facet_wrap
standardized plot:
melteddf %>% ggplot(aes(y = depth , x = value)) + geom_path() + facet_grid(site~variable , scales = "free_x") + scale_y_reverse()
Comments
Post a Comment