I have the ctlns list and I am trying to produce some visualization of the data
ctlns<-list(structure(list(level = 10, x = c(0.101666666666667, 0.06,
0.0385714285714286, 0.035, 0.035, 0.035, 0.04, 0.0433333333333333,
0.05, 0.0516666666666667, 0.06, 0.0606416584402764, 0.0606416584402764,
0.0766666666666667, 0.0766666666666667, 0.0933333333333333, 0.0933333333333333,
0.0975, 0.11, 0.110956351152526, 0.110956351152526, 0.135, 0.135
), y = c(0.01, 0.04125, 0.06, 0.11, 0.16, 0.21, 0.26, 0.31, 0.36,
0.41, 0.458123195380173, 0.46, 0.51, 0.56, 0.61, 0.66, 0.71,
0.76, 0.808123195380173, 0.81, 0.86, 0.91, 0.96)), .Names = c("level",
"x", "y")))
Then I,
plot(ctlns[[1]]$x,ctlns[[1]]$y, xlim=c(0,.21), ylim=c(0,1), lwd=2, type="l", col="darkred" )
And I get the plot
I would like to smooth the upper part of the red curve (y>0.2) while maintaining some of the curved structure (y<0.2)
lines(lowess(ctlns[[1]]$x,ctlns[[1]]$y,f=2/3), lwd=2, col="darkblue")
does a fine job for the former part but deletes the lower part of the curve. I have the following questions:
Why does this happen that?
How can I preserve and smooth the lower part of the red curve? Or maybe combine curves/smooth lines?
Ignoring the red curve, how can I instruct lowess based on the blue curve data to extrapolate the values till y=0?
EDIT after discussion with agstudy
Because of of the curved nature of the red line, I was thinking what I need is probably a not a function smoothing y~x but rather a graph function that connects the points x, y with some kind of curved line. The points should be connected in order they appear within their vectors (x[1] with y[1] and so on...)
Is this possible?
You probably want to use the xspline function (or grid.xspline if using grid graphics).
plot( ctlns[[1]], type='l', col='red' )
xspline( ctlns[[1]], shape=1, border='blue' )
You can do some pre smoothing of the data which might help some as well:
tmp.x <- ctlns[[1]]$x
tmp.y <- ctlns[[1]]$y
tmp <- cumsum( c(TRUE, head(tmp.x,-1) != tail(tmp.x,-1) ) )
tmp2.x <- tapply( tmp.x, tmp, mean )
tmp2.y <- tapply( tmp.y, tmp, mean )
xspline( tmp2.x, tmp2.y, shape=1, border='green' )
or using loess for the smoothing:
fit <- loess( tmp.y ~ tmp.x+tmp )
tmp3.y <- tapply( fitted(fit), tmp, mean )
xspline( tmp2.x, tmp3.y, shape=1, border='orange' )
to answer part 2 of your question:
lines(lowess(ctlns[[1]]$x[ctlns[[1]]$y<0.2],
ctlns[[1]]$y[ctlns[[1]]$y<0.2]), lwd=2, col="darkblue")
For the first part of your question , I guess that the algorithm is designed to work on function (mathematical defintion of the term) it removes the duplicates on x.
Edit after OP comment!
for me this is good , at least that I use LOESS function in an optimal manner.
If you want to join all parts you create a small line for points that create problem.
ids <- duplicated(ctlns[[1]]$x) & ctlns[[1]]$y < 0.25
lines(ctlns[[1]]$x[ids],ctlns[[1]]$y[ids], lwd=4, col="darkblue")
Related
I have created the following plot using plot() function and I would like to convert it to ggplot() and add colors in the line types like:
and also a legend for 'predicted' (normal line) and 'observed' values (dashed line) like:
Here is my code:
# Creating some data first
scoregroepen <- seq(from = 1, to = 8, by = 1)
s_toets_observed <- c(0.18, 0.31, 0.42, 0.53, 0.64,0.75,0.84,0.95)
s_toets_predicted <- c(0.20, 0.29, 0.40, 0.55, 0.66, 0.75, 0.85, 0.94)
s_toets_conf_low <- s_toets_observed-0.03
s_toets_conf_high <- s_toets_observed+0.045
plot(scoregroepen,s_toets_predicted, type="b", ylab = "proporties", ylim = c(0,1))
lines(scoregroepen, s_toets_observed, type="b", lty = 2 )
lines(scoregroepen, s_toets_conf_low, lty = 2 )
lines(scoregroepen, s_toets_conf_high, lty = 2 )
Try this which is close to what you expect. I have re arranged your variables in a dataframe to reshape them and then sketch the plot. Here the code:
library(ggplot2)
library(dplyr)
library(tidyr)
# Creating some data first
scoregroepen <- seq(from = 1, to = 8, by = 1)
s_toets_observed <- c(0.18, 0.31, 0.42, 0.53, 0.64,0.75,0.84,0.95)
s_toets_predicted <- c(0.20, 0.29, 0.40, 0.55, 0.66, 0.75, 0.85, 0.94)
s_toets_conf_low <- s_toets_observed-0.03
s_toets_conf_high <- s_toets_observed+0.045
df <- data.frame(scoregroepen,s_toets_observed,s_toets_predicted,
s_toets_conf_low,s_toets_conf_high)
#Plot
df %>% pivot_longer(-scoregroepen) %>%
ggplot(aes(x=scoregroepen,y=value,color=name,linetype=name))+
geom_line()+
geom_point(aes(shape=name))+
scale_color_manual(values=c('blue','blue','tomato','cyan3'),
breaks=c('s_toets_observed','s_toets_predicted'),
labels=c('Observed','Predicted'))+
scale_shape_manual(values=c(NA,NA,1,4),
breaks=c('s_toets_observed','s_toets_predicted'),
labels=c('Observed','Predicted'))+
scale_linetype_manual(values=c('dotted','dotted','dashed','solid'),
breaks=c('s_toets_observed','s_toets_predicted'),
labels=c('Observed','Predicted'))+
labs(color='var',shape='var',linetype='var')
Output:
I'd like to eliminate the white space between my two forest plots that I plotted side-by-side using grid.arrange().
Before you vote down or redirect - Before asking this question, I have spent hours attempting every solution posed in each of the responses I've seen here for similar questions without achieving my desired result.
First, here is my dataset and code:
library(meta)
library(grid)
library(gridExtra)
df <- structure(list(study = 1:7,
sens = c(0.88, 0.86, 0.75, 0.9, 0.91, 0.93, 0.98),
sens.se = c(0.13, 0.08, 0.2, 0.06, 0.13, 0.15, 0.66),
sens2 = c(0.76, 0.68, 0.9, 0.82, 0.76, 0.85, 0.76),
sens.se2 = c(0.14, 0.08, 0.2, 0.06, 0.14, 0.15, 0.66)),
class = "data.frame",
row.names = c(NA, -7L))
## setting up meta-analysis model using library(meta)
res1 <- metagen(TE=sens, seTE=sens.se, data=df, studlab=study)
res2 <- metagen(TE=sens2, seTE=sens.se2, data=df, studlab=study)
## changing plots to grid graphical objects to use grid.arrange
fp1 <- grid.grabExpr(forest(res1, data=df, method.tau="REML",
comb.random=TRUE, leftcols="studlab",
rightcols=c("effect", "ci")))
fp2 <- grid.grabExpr(forest(res2, data=df, method.tau="REML",
comb.random=TRUE, leftcols="studlab",
rightcols=c("effect", "ci")))
## arranging plots side by side:
grid.arrange(fp1, fp2, ncol = 2)
When I have attempted to use code suggested in responses to similar questions, I get the "only grobs allowed in gList" error code, even though R recognizes the plots as "gTrees" because I used the grid.grabExpr function. I've tried coercing the gTrees into grobs via:
p1 <- as.grob(fp1)
p2 <- as.grob(fp2)
, which only creates null values in the global environment.
I would greatly appreciate some help with this!
Perhaps this does what you are looking for:
grid.grabExpr(
forest(
res1, data=df, method.tau="REML",
comb.random=TRUE, leftcols="studlab",
rightcols=c("effect", "ci")
),
height = 1, width = 2
) -> fp1
grid.grabExpr(
forest(
res2, data=df, method.tau="REML",
comb.random=TRUE, leftcols="studlab",
rightcols=c("effect", "ci")
),
height = 1, width = 2
) -> fp2
grid.arrange(fp1, fp2, ncol = 2, vp=viewport(width=1, height=1, clip = TRUE))
I have been trying to create a density plot in R that looks similar to the picture below.
In my code below, I have created a stat_density_2D plot that successfully plots my data, however, it fails to recognize my fill variable (in this case exitspeed) and only plots one color.
Upon further research, I believe the reason for this is because stat_density_2d bins the fill into levels. The problem I am having is that my fill variable has multiple values for the points within a particular level ultimately resulting in a density plot that only displays one color. Does anyone know how to bin my data so that my density plot can recognize the fill variable (exitspeed)? Please see below for the dataset and R code. Thanks in advance!
Data:
structure(list(platelocheight = c(2.594, 3.803, 3.254, 3.599,
3.617, 3.297, 2.093, 3.611, 2.842, 3.316, 2.872, 3.228, 3.633,
4.28, 3.309, 2.8, 2.632, 3.754, 2.207, 3.604, 3.443, 2.188, 3.452,
2.553, 3.382, 3.067, 2.986, 2.785, 2.567, 3.804), platelocside = c(0.059,
-1.596, -0.65, -0.782, -0.301, -0.104, 0.057, -0.807, 0.003,
1.661, 0.088, -0.32, -1.115, -0.146, -0.364, -0.952, 0.254, 0.109,
-0.671, -0.803, -0.212, -0.069, -0.09, -0.472, 0.434, 0.337,
0.723, 0.508, -0.197, -0.635), exitspeed = c(69.891, 73.352,
83.942, 85.67, 79.454, 85.277, 81.078, 73.573, 77.272, 59.263,
97.343, 91.436, 76.264, 83.479, 47.576, 84.13, 60.475, 61.093,
84.54, 69.959, 88.729, 88.019, 82.18, 83.684, 86.296, 90.605,
79.945, 59.899, 62.522, 77.75)), .Names = c("platelocheight",
"platelocside", "exitspeed"), row.names = c(NA, 30L), class = "data.frame")
R-Code:
library(RODBC)
library(ggplot2)
con=odbcConnect('username',uid='userid', pwd = 'password')
df=sqlQuery(con,"select platelocheight, platelocside, exitspeed from tm_sample where pitchcall='InPlay'
and exitspeed is not null")
topKzone <- 3.5
botKzone <- 1.6
inKzone <- -0.95
outKzone <- 0.95
kZone <- data.frame(
x=c(inKzone, inKzone, outKzone, outKzone, inKzone),
y=c(botKzone, topKzone, topKzone, botKzone, botKzone)
)
df$h <- round(df$platelocheight)
df$s <- round(df$platelocside)
df$es<- round(df$exitspeed)
ggplot(kZone, aes(x,y)) +
stat_density_2d(data=df, aes(x=s, y=h, fill=es),geom="polygon") +
scale_fill_distiller(palette = "Spectral") +
geom_path(lwd=1.5, col="black") +
coord_fixed()
Hello guys and thanks in advance for your time.
I'm trying to create a circular plot with a color map that should be related with different temperature values acquired on a disk in order to verify the homogeneity of the heating, but despite my numerous trial and efforts it just wont work as i'd like it to be (i'm a newbie in programming).
Any ideas?
Thanks for your attention and have a nice day!
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
x = np.arange(-2, 2, 0.01)
y = np.arange(-2, 2, 0.01)
X, Y = np.meshgrid(x, y)
Z = X**2 + Y**2 + 1 #np.sin(X) * np.cos(Y) * 2
R = 4
Z[Z>R] = 0
colors = [(1,1,1), (0.99, 0.90, 0.68), (1, 0.87, 0.58), (0.93, 0.79, 0.53), (0.97, 0.71, 0.35), (0.84, 0.71, 0.27), (0.74, 0.48, 0.23),
(0.65, 0.44, 0.24), (0.56, 0.39, 0.23), (0.48, 0.32, 0.23),( 0.84, 0.53, 0.20), (0.21, 0.2, 0.17)] # R -> G -> B
n_bins = [100]
cmap_name = 'my_list'
fig, axs = plt.subplots(2, figsize=(6, 9))
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
for n_bin, ax in zip(n_bins, axs.ravel()):
cm = LinearSegmentedColormap.from_list(
cmap_name, colors, N=n_bin)
im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=cm)
ax.set_title("N bins: %s" % n_bin)
fig.colorbar(im, ax=ax)
This is a code that i'm trying to modify in order fit my purpose (temperature values correlation is still missing, i know...).
I have a series of values with a mean and a 2sd error:
structure(list(Site = 1:5, Value = c(0.54, 0.36, 0.13, 0.25,
0.05), Error = c(0.26, 0.27, 0.25, 0.4, 0.24)), .Names = c("Site",
"Value", "Error"), class = "data.frame", row.names = c(NA, -5L
))
I am trying to represent this a series of normal curves on one graph where the mid point of the curve is the mean and the range of the base of the curve is the mean+error/mean-error. The height of the curves can all be the same as we give each mean value the same weight.
I've had a search and I am really stuck. Sorry if I am missing somewhere where this may have been answered.
First you need to set up the plot but give 'plot' an NA to suppress any plotting. When you do that, plot requires ranges for X and Y
plot(NA, xlim=c( min(dat$Value)-max(dat$Error),
max(dat$Value)+max(dat$Error) ),
ylim=c(0,1) )
apply(dat, 1, function(x){ xx <-seq( x['Value']-x['Error'],
x['Value']+x['Error'], length=20);
yy=dnorm(xx, x['Value'], x['Error']/2); sd is 1/2 'Error'
lines(xx, yy/max(yy)) }) # normalize to peak == 1
If you want a smoother plot near the means, you can always increase the length of the 'xx' sequence.