Circular plot with related temperature - python-3.6

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...).

Related

Add legend based on line types and colors in ggplot

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:

Remove white space between two plots resulting from grid.arrange function in R

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))

Plotting climate data with NetCdf files for a specific region

I can plot temperature distribution figures with global NetCdf files with these codes.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset as dt
import numpy as np
import matplotlib.pyplot as plt
filestr='E:/VIC/Forcing Data from princeton/from 48 to 2016/01.tmax/tmax_daily_2000-2000.nc'
ncfile=dt(filestr, 'r')
lats = ncfile.variables['lat'][:]
lons = ncfile.variables['lon'][:]
time = np.array(ncfile.variables['time'][:], dtype=np.float64)
data = ncfile.variables['tmax'][300,:,:]
data -=273
# Set font name
plt.rcParams["font.family"] = "cambria"
# Add Title
plt.suptitle('sub title', fontsize=12, fontweight='bold') #<---------
plt.title('title' , fontsize=12) #<---------
# Add basemap
map = Basemap(projection='merc',llcrnrlon=30,llcrnrlat=24,urcrnrlon=53,urcrnrlat=43,resolution='i', epsg = 4269)
# projection, lat/lon extents and resolution of polygons to draw
# resolutions: c - crude, l - low, i - intermediate, h - high, f - full
#map.drawmapscale()
map.arcgisimage(service='World_Physical_Map', xpixels = 5000, verbose= False)
map.drawcoastlines(linewidth=0.3, color='xkcd:darkblue')
#map.drawstates(linewidth=0.8)
#map.drawcountries(color ='r')
#map.drawlsmask(land_color='Linen', ocean_color='#CCFFFF') # can use HTML names or codes for colors
#map.drawcounties() # you can even add counties (and other shapefiles!)
parallels = np.arange(24.125,42.125,25.) # make latitude lines ever 5 degrees from 30N-50N #<---------
meridians = np.arange(32.125,52.375,25.) # make longitude lines every 5 degrees from 95W to 70W #<---------
map.drawparallels(parallels,linewidth=0.3,labels=[1,0,0,0],fontsize=10, color='white')
map.drawmeridians(meridians,linewidth=0.3,labels=[0,0,0,1],fontsize=10, color='white')
map.readshapefile('C:/Users/fyunu/OneDrive/Masaüstü/ETB STUDY/Shape File Area of the ETB basin/Aqueduct_river_basins_TIGRIS & EUPHRATES', \
name='Aqueduct_river_basins_TIGRIS & EUPHRATES', drawbounds=True, linewidth=0.6) #<---------
lon,lat= np.meshgrid(lons,lats) #(lons-360.,lats) # for this dataset, longitude is 0 through 360, so you need to subtract 180 to properly display on map
xi,yi = map(lon,lat) #<---------
levels = [i for i in range(-20, 70, 5)]
#levels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.1, 1.5, 2.]
#levels = [-1., 0.6, 0.80, 0.85, 0.90, 0.95, 1, 1.05, 1.1, 1.15, 1.2, 1.4, 5]
#cs = map.pcolor(xi,yi,var,cmap='jet',vmin=min_value, vmax=max_value) #'RdBu_r')
map.contour(xi, yi, data, levels, linewidths=0.1, colors='k', linestyles='solid')
cs = map.contourf(xi, yi, data, levels, cmap=plt.get_cmap('jet'))#cmap=plt.cm.jet) #colors=colors_range) #,vmin=min_value, vmax=max_value) #<---------
# Add Colorbar
cbar = map.colorbar(cs, location='right', size='5%',pad="1%")
cbar.set_label('unit') #('Percent’) #<---------
plt.show()
#plt.savefig(path + 'mapPlot_' + plotInfo.variable + '_' + title + '_' + plotInfo.legend + '.png',transparent=True, dpi=300) #<---------
plt.close()
But I want to select only coordinates of the specific region. I have those coordinates in a csv file. Csv file has one column as 'lons' and one column as 'lats'. I want to read these columns and plot the data distribution according to these coordinates.
I tried
inpExcelFile = 'C:/Users/fyunu/OneDrive/Masaüstü/gridCellCoordinates6 seperately.csv' #lat, lon
df1 = pd.read_csv(inpExcelFile)
lats = float(df1.columns['lats'][:])
lons = float(df1.columns['lons'][:])
But I got OSError: Initializing from file failed.

Increase line width without stochastic bars ggplot

Does anyone know if it's possible to increase the line width in ggplot2 in a smooth fashion without adding random lines that stick out? Here's my original line plot and with size increased to 5:
> ggplot(curve.df, aes(x=recall, y=precision, color=cutoff)) +
> geom_line(size=1)
Ideally, the final image would look something like the following plot from the PRROC Package, but I have another problem with plotting from there in that gridlines and ablines do not correspond to the axis tickmarks.
Here I first called
> grid()
and then called
> abline(v=seq(0,1,.2), h=seq(0,1,.2))
Honestly would appreciate any way to be able to draw this curve with a wider line to see clear colors and a grid that corresponds to the axis tickmarks. Thanks!
Here's a sample of the data from cutoff .5 to .7:
> dput(output)
structure(list(recall = c(0.0237648530331457, 0.024390243902439,
0.0250156347717323, 0.0256410256410256, 0.0256410256410256, 0.0268918073796123,
0.0275171982489056, 0.0281425891181989, 0.0293933708567855, 0.0300187617260788,
0.0300187617260788, 0.0300187617260788, 0.0306441525953721, 0.0312695434646654,
0.0312695434646654, 0.0312695434646654, 0.0318949343339587, 0.0318949343339587,
0.0318949343339587, 0.032520325203252, 0.0331457160725453, 0.0331457160725453,
0.0337711069418387, 0.034396497811132, 0.034396497811132, 0.0350218886804253,
0.0356472795497186, 0.0356472795497186, 0.0362726704190119, 0.0362726704190119,
0.0362726704190119, 0.0387742338961851, 0.0387742338961851, 0.0387742338961851,
0.0393996247654784, 0.0400250156347717, 0.0400250156347717, 0.040650406504065,
0.040650406504065, 0.040650406504065, 0.0412757973733583, 0.0419011882426517,
0.042526579111945, 0.0431519699812383, 0.0431519699812383, 0.0437773608505316,
0.0444027517198249, 0.0450281425891182, 0.0456535334584115, 0.0456535334584115,
0.0462789243277048, 0.0469043151969981, 0.0469043151969981, 0.0469043151969981,
0.0469043151969981, 0.0475297060662914, 0.0481550969355847, 0.0481550969355847,
0.0494058786741714, 0.0494058786741714, 0.0494058786741714, 0.0494058786741714,
0.0512820512820513, 0.0512820512820513, 0.0531582238899312, 0.0537836147592245,
0.0537836147592245, 0.0537836147592245, 0.0550343964978111, 0.0556597873671044,
0.0556597873671044, 0.0562851782363977, 0.0569105691056911, 0.0575359599749844,
0.0581613508442777, 0.058786741713571, 0.0594121325828643, 0.0594121325828643,
0.0600375234521576, 0.0606629143214509, 0.0612883051907442, 0.0625390869293308,
0.0631644777986241, 0.0637898686679174, 0.0644152595372108, 0.0644152595372108,
0.0644152595372108, 0.0650406504065041, 0.0650406504065041, 0.0656660412757974,
0.0656660412757974, 0.0662914321450907, 0.066916823014384, 0.0687929956222639,
0.0694183864915572, 0.0700437773608505, 0.0700437773608505, 0.0706691682301438,
0.0712945590994371, 0.0712945590994371, 0.0712945590994371, 0.0712945590994371,
0.0712945590994371, 0.0712945590994371, 0.0719199499687305, 0.0725453408380238,
0.0725453408380238, 0.0731707317073171, 0.075046904315197, 0.075046904315197,
0.0756722951844903, 0.0762976860537836, 0.0769230769230769, 0.0775484677923702,
0.0775484677923702, 0.0787992495309568, 0.0794246404002502, 0.0794246404002502,
0.0794246404002502, 0.0800500312695435, 0.0800500312695435, 0.0806754221388368,
0.0813008130081301, 0.0813008130081301, 0.0819262038774234, 0.0825515947467167,
0.08317698561601, 0.08317698561601, 0.0850531582238899, 0.0863039399624766,
0.0863039399624766, 0.0869293308317699, 0.0881801125703565, 0.0888055034396498,
0.0888055034396498, 0.0900562851782364, 0.0919324577861163, 0.0919324577861163,
0.0925578486554096, 0.0931832395247029, 0.0931832395247029, 0.0931832395247029,
0.0938086303939962, 0.0944340212632895, 0.0944340212632895, 0.0956848030018762,
0.0956848030018762, 0.0963101938711695, 0.0963101938711695, 0.0963101938711695,
0.0963101938711695, 0.0975609756097561, 0.0981863664790494, 0.0988117573483427,
0.0988117573483427, 0.099437148217636, 0.099437148217636, 0.100062539086929,
0.100687929956223, 0.101313320825516, 0.103189493433396, 0.103814884302689,
0.103814884302689, 0.103814884302689, 0.105065666041276, 0.105691056910569,
0.106316447779862, 0.106316447779862, 0.106941838649156, 0.107567229518449,
0.107567229518449, 0.107567229518449, 0.108192620387742, 0.108818011257036,
0.109443402126329, 0.110694183864916, 0.110694183864916, 0.111319574734209,
0.111319574734209, 0.111319574734209, 0.112570356472795, 0.114446529080675,
0.114446529080675, 0.114446529080675, 0.114446529080675, 0.115071919949969,
0.115697310819262, 0.118198874296435, 0.119449656035022, 0.119449656035022,
0.119449656035022, 0.120700437773609, 0.120700437773609, 0.121325828642902,
0.121951219512195, 0.121951219512195, 0.122576610381488, 0.122576610381488,
0.122576610381488, 0.122576610381488, 0.123202001250782, 0.123827392120075,
0.125703564727955, 0.127579737335835, 0.127579737335835, 0.127579737335835,
0.127579737335835, 0.127579737335835, 0.128830519074422, 0.128830519074422,
0.129455909943715, 0.129455909943715, 0.130706691682301, 0.131957473420888,
0.132582864290181, 0.132582864290181, 0.134459036898061, 0.135084427767355,
0.136335209505941, 0.136960600375235, 0.136960600375235, 0.136960600375235,
0.137585991244528, 0.138211382113821, 0.138211382113821, 0.138836772983114,
0.140712945590994, 0.140712945590994, 0.141338336460288, 0.141338336460288,
0.141963727329581, 0.141963727329581, 0.149468417761101), precision = c(0.584615384615385,
0.590909090909091, 0.597014925373134, 0.602941176470588, 0.594202898550725,
0.597222222222222, 0.594594594594595, 0.6, 0.602564102564103,
0.607594936708861, 0.6, 0.592592592592593, 0.597560975609756,
0.595238095238095, 0.588235294117647, 0.581395348837209, 0.579545454545455,
0.573033707865168, 0.566666666666667, 0.571428571428571, 0.56989247311828,
0.563829787234043, 0.568421052631579, 0.572916666666667, 0.56701030927835,
0.571428571428571, 0.575757575757576, 0.57, 0.568627450980392,
0.563106796116505, 0.557692307692308, 0.553571428571429, 0.548672566371681,
0.543859649122807, 0.538461538461538, 0.542372881355932, 0.53781512605042,
0.541666666666667, 0.537190082644628, 0.532786885245902, 0.536585365853659,
0.540322580645161, 0.544, 0.543307086614173, 0.5390625, 0.538461538461538,
0.537878787878788, 0.537313432835821, 0.540740740740741, 0.536764705882353,
0.536231884057971, 0.531914893617021, 0.528169014084507, 0.524475524475524,
0.520833333333333, 0.524137931034483, 0.523809523809524, 0.52027027027027,
0.526666666666667, 0.52317880794702, 0.516339869281046, 0.512987012987013,
0.522292993630573, 0.518987341772152, 0.527950310559006, 0.52760736196319,
0.524390243902439, 0.521212121212121, 0.526946107784431, 0.529761904761905,
0.526627218934911, 0.526315789473684, 0.526011560693642, 0.528735632183908,
0.531428571428571, 0.531073446327684, 0.527777777777778, 0.524861878453039,
0.524590163934426, 0.527173913043478, 0.52972972972973, 0.529100529100529,
0.528795811518325, 0.525773195876289, 0.528205128205128, 0.525510204081633,
0.517587939698492, 0.52, 0.517412935323383, 0.51980198019802,
0.51219512195122, 0.514563106796116, 0.514423076923077, 0.52132701421801,
0.523584905660377, 0.523364485981308, 0.52093023255814, 0.518348623853211,
0.520547945205479, 0.518181818181818, 0.515837104072398, 0.511210762331839,
0.508928571428571, 0.506666666666667, 0.508849557522124, 0.5,
0.497854077253219, 0.497872340425532, 0.504201680672269, 0.502092050209205,
0.504166666666667, 0.506224066390041, 0.506172839506173, 0.508196721311475,
0.506122448979592, 0.510121457489879, 0.51004016064257, 0.508,
0.503968253968254, 0.498054474708171, 0.496124031007752, 0.494252873563218,
0.492424242424242, 0.490566037735849, 0.488805970149254, 0.488888888888889,
0.488970588235294, 0.487179487179487, 0.489208633093525, 0.48936170212766,
0.487632508833922, 0.48943661971831, 0.493006993006993, 0.491349480968858,
0.487972508591065, 0.491467576791809, 0.496621621621622, 0.493288590604027,
0.486842105263158, 0.486928104575163, 0.485342019543974, 0.482200647249191,
0.482315112540193, 0.482428115015974, 0.480891719745223, 0.481132075471698,
0.479623824451411, 0.48125, 0.479750778816199, 0.478260869565217,
0.476780185758514, 0.478527607361963, 0.480122324159021, 0.480243161094225,
0.478787878787879, 0.478915662650602, 0.477477477477477, 0.479041916167665,
0.476331360946746, 0.47787610619469, 0.478260869565217, 0.479768786127168,
0.478386167146974, 0.474285714285714, 0.473239436619718, 0.472067039106145,
0.472222222222222, 0.470914127423823, 0.472375690607735, 0.471232876712329,
0.464864864864865, 0.463611859838275, 0.46505376344086, 0.466487935656836,
0.467914438502674, 0.468253968253968, 0.467018469656992, 0.467191601049869,
0.464751958224543, 0.463541666666667, 0.465116279069767, 0.465648854961832,
0.464467005076142, 0.462121212121212, 0.46095717884131, 0.462311557788945,
0.461346633416459, 0.466666666666667, 0.466992665036675, 0.465853658536585,
0.463592233009709, 0.463942307692308, 0.462829736211031, 0.463007159904535,
0.464285714285714, 0.463182897862233, 0.462264150943396, 0.460093896713615,
0.456876456876457, 0.455813953488372, 0.454965357967667, 0.456221198156682,
0.457858769931663, 0.461538461538462, 0.460496613995485, 0.458426966292135,
0.453333333333333, 0.452328159645233, 0.45374449339207, 0.452747252747253,
0.453947368421053, 0.451965065502183, 0.453362255965293, 0.455723542116631,
0.456896551724138, 0.455913978494624, 0.459401709401709, 0.460554371002132,
0.461864406779661, 0.463002114164905, 0.461052631578947, 0.460084033613445,
0.460251046025105, 0.459459459459459, 0.457556935817805, 0.457731958762887,
0.458248472505092, 0.456389452332657, 0.456565656565657, 0.455645161290323,
0.455823293172691, 0.454909819639279, 0.449248120300752), cutoff = c(0.7,
0.695652173913043, 0.694444444444444, 0.694117647058824, 0.693333333333333,
0.692307692307692, 0.691358024691358, 0.691176470588235, 0.690140845070423,
0.689655172413793, 0.688888888888889, 0.688311688311688, 0.6875,
0.686746987951807, 0.686567164179104, 0.686046511627907, 0.685714285714286,
0.684210526315789, 0.683544303797468, 0.683333333333333, 0.680555555555556,
0.68, 0.67948717948718, 0.67741935483871, 0.676923076923077,
0.676056338028169, 0.675675675675676, 0.675324675324675, 0.671641791044776,
0.671428571428571, 0.671052631578947, 0.666666666666667, 0.662650602409639,
0.662162162162162, 0.661764705882353, 0.661538461538462, 0.658536585365854,
0.657894736842105, 0.657534246575342, 0.657142857142857, 0.65625,
0.653846153846154, 0.653333333333333, 0.652777777777778, 0.652173913043478,
0.650602409638554, 0.65, 0.648648648648649, 0.647887323943662,
0.647058823529412, 0.645569620253165, 0.643835616438356, 0.64367816091954,
0.642857142857143, 0.641975308641975, 0.640625, 0.64, 0.639344262295082,
0.638888888888889, 0.63855421686747, 0.63768115942029, 0.6375,
0.636363636363636, 0.635135135135135, 0.633802816901408, 0.631578947368421,
0.63013698630137, 0.62962962962963, 0.628571428571429, 0.627118644067797,
0.626865671641791, 0.625, 0.623529411764706, 0.622950819672131,
0.622222222222222, 0.621951219512195, 0.621621621621622, 0.621212121212121,
0.619718309859155, 0.619047619047619, 0.618421052631579, 0.617647058823529,
0.617283950617284, 0.616438356164384, 0.615384615384615, 0.614035087719298,
0.613333333333333, 0.6125, 0.611940298507463, 0.611764705882353,
0.61038961038961, 0.609375, 0.609195402298851, 0.608695652173913,
0.608108108108108, 0.607594936708861, 0.606060606060606, 0.605633802816901,
0.605263157894737, 0.604938271604938, 0.604651162790698, 0.602941176470588,
0.602739726027397, 0.602564102564103, 0.602272727272727, 0.6,
0.597826086956522, 0.597402597402597, 0.597222222222222, 0.597014925373134,
0.596774193548387, 0.594936708860759, 0.594202898550725, 0.593220338983051,
0.592592592592593, 0.591549295774648, 0.590909090909091, 0.590361445783133,
0.588235294117647, 0.586666666666667, 0.585365853658537, 0.584615384615385,
0.583333333333333, 0.582278481012658, 0.582089552238806, 0.581081081081081,
0.580645161290323, 0.580246913580247, 0.579710144927536, 0.578947368421053,
0.578313253012048, 0.578125, 0.577464788732394, 0.576923076923077,
0.575342465753425, 0.575, 0.573333333333333, 0.573170731707317,
0.571428571428571, 0.569620253164557, 0.569230769230769, 0.568965517241379,
0.567901234567901, 0.567567567567568, 0.567164179104478, 0.565789473684211,
0.565217391304348, 0.564705882352941, 0.563380281690141, 0.5625,
0.561797752808989, 0.561643835616438, 0.560975609756098, 0.560606060606061,
0.55952380952381, 0.558823529411765, 0.558441558441558, 0.557377049180328,
0.557142857142857, 0.556962025316456, 0.555555555555556, 0.55421686746988,
0.554054054054054, 0.552631578947368, 0.552238805970149, 0.551282051282051,
0.550724637681159, 0.55, 0.549295774647887, 0.547945205479452,
0.546666666666667, 0.546511627906977, 0.544117647058823, 0.54320987654321,
0.542168674698795, 0.541666666666667, 0.541176470588235, 0.540540540540541,
0.53968253968254, 0.539473684210526, 0.538461538461538, 0.537313432835821,
0.536585365853659, 0.536231884057971, 0.535714285714286, 0.535211267605634,
0.534246575342466, 0.533333333333333, 0.532467532467532, 0.531645569620253,
0.53125, 0.529411764705882, 0.528571428571429, 0.527777777777778,
0.527272727272727, 0.527027027027027, 0.526315789473684, 0.525641025641026,
0.525, 0.524590163934426, 0.524390243902439, 0.523255813953488,
0.523076923076923, 0.522388059701492, 0.521739130434783, 0.52112676056338,
0.520547945205479, 0.52, 0.519480519480519, 0.518987341772152,
0.518518518518518, 0.518072289156627, 0.517647058823529, 0.515151515151515,
0.514285714285714, 0.513888888888889, 0.513513513513513, 0.513157894736842,
0.512820512820513, 0.5125, 0.51219512195122, 0.511627906976744,
0.508196721311475, 0.507692307692308, 0.507462686567164, 0.507246376811594,
0.507042253521127, 0.506849315068493, 0.506666666666667, 0.506493506493506,
0.506329113924051, 0.505747126436782, 0.5)), .Names = c("recall",
"precision", "cutoff"), row.names = 55:287, class = "data.frame")
Setting lineend = "round" greatly improves the plot
ggplot(curve.df, aes(x = recall, y = precision, color = cutoff)) +
geom_line(size = 5, lineend = "round")
ggplot can't plot a single line with multiple colors. The "stochastic" bits of your plot are actually the tops and bottoms of super little short lines (that are much thicker than they are long) connecting points that are close enough together in cutoff to share the same color.
Luckily, your data is so dense, a line plot is actually unnecessary. We can just plot points and all the problems go away - if we make them big enough, which seems to be what you want. (You will see the individual points if your zoom in on the data excerpt provided, but I expanded the limits to make show the data density on the size of plot you are really using. The average difference in recall between adjacent points is .00054, so on the scale of 0 to 1 your data is very dense!)
I also show a version with a loess smoother - you can of course play with the bandwidth for more or less smoothing. This may or may not be preferable.
raw_plot = ggplot(df, aes(recall, precision, color = cutoff)) +
geom_point(size = 3) +
coord_fixed(xlim = c(0, 1), ylim = c(0, 1)) +
labs(title = "Raw")
df$smooth = predict(loess(precision ~ recall, data = df))
smooth_plot = ggplot(df, aes(recall, smooth, color = cutoff)) +
geom_point(size = 3) +
coord_fixed(xlim = c(0, 1), ylim = c(0, 1)) +
labs(title = "Smooth")
gridExtra::grid.arrange(raw_plot, smooth_plot, nrow = 1)

How to connect points with curved and smooth lines

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")

Resources