ggplot2 black bar when outputting plot using bbplot theme - r

I've started using the bbplot package today which is generally working really well, and is a theme and wrapper for ggplot2 created by the BBC. However, when finalising the plot, the exports seem to include a black bar at the bottom of the image which masks the source details. I've fiddled with the code and it seems to be happening when the subtitle or source are longer than a certain number of characters (although still less wide than the width of the available space). I can't get any plots to finalise with both a subtitle and source at present.
Using databricks in azure - Runtime 9.1 LTS
Code:
svannualcount <- ggplot(svkc_count, aes(x = as.character(Recorded_Year), y = n)) +
geom_col(fill="#082a52") +
geom_hline(yintercept = 0, size = 1, colour="#333333") +
theme(axis.text.x = element_text(angle = 0)) +
bbc_style() +
labs(title="This is a test title",
subtitle = "Can you read my test title?")
finalise_plot(plot_name = svannualcount,
source = "Source: This is a test source.",
save_filepath = "/tmp/svkcannualplot.png",
width_pixels = 640,
height_pixels = 450,
logo_image_path = "/tmp/vrulogo.png"
)
Data:
Recorded_Year n `as.character(Recorded_Year)`
<int> <dbl> <chr>
1 2018 2171 2018
2 2015 1128 2015
3 2019 2128 2019
4 2020 2041 2020
5 2016 1572 2016
6 2017 1968 2017
7 2021 1711 2021
Image:

On further testing and with useful input from #rui-barradas this was identified as an issue with databricks and ggplot2 plots not appearing correctly in line in a notebook. The exported plots render correctly when opened in an image browser.

Related

How to get years from a time series when the plot is displaying time series of a monthly freq (Rbeast )

I´d like to extract years from a time series index (the underlying time series is of monthly frequency). The reason I want to do it is creating a yearly axis.
I am using a package Rbeast.
Here is my result for tsp(NDVI_Chobe001.ts)
[1] 2002.500 2020.417 12.000
##Here is a more detailed time series
time(NDVI_Chobe001.ts)
Jan Feb Mar Apr May Jun Jul Aug Sep
2002 2002.500 2002.583 2002.667
2003 2003.000 2003.083 2003.167 2003.250 2003.333 2003.417 2003.500 2003.583 2003.667
2004 2004.000 2004.083 2004.167 2004.250 2004.333 2004.417 2004.500 2004.583 2004.667
2005 2005.000 2005.083 2005.167 2005.250 2005.333 2005.417 2005.500 2005.583 2005.667
2006 2006.000 2006.083 2006.167 2006.250 2006.333 2006.417 2006.500 2006.583 2006.667
2007 2007.000 2007.083 2007.167 2007.250 2007.333 2007.417 2007.500 2007.583 2007.667
2008 2008.000 2008.083 2008.167 2008.250 2008.333 2008.417 2008.500 2008.583 2008.667
2009 2009.000 2009.083 2009.167 2009.250 2009.333 2009.417 2009.500 2009.583 2009.667
2010 2010.000 2010.083 2010.167 2010.250 2010.333 2010.417 2010.500 2010.583 2010.667
2011 2011.000 2011.083 2011.167 2011.250 2011.333 2011.417 2011.500 2011.583 2011.667
2012 2012.000 2012.083 2012.167 2012.250 2012.333 2012.417 2012.500 2012.583 2012.667
2013 2013.000 2013.083 2013.167 2013.250 2013.333 2013.417 2013.500 2013.583 2013.667
2014 2014.000 2014.083 2014.167 2014.250 2014.333 2014.417 2014.500 2014.583 2014.667
2015 2015.000 2015.083 2015.167 2015.250 2015.333 2015.417 2015.500 2015.583 2015.667
2016 2016.000 2016.083 2016.167 2016.250 2016.333 2016.417 2016.500 2016.583 2016.667
2017 2017.000 2017.083 2017.167 2017.250 2017.333 2017.417 2017.500 2017.583 2017.667
2018 2018.000 2018.083 2018.167 2018.250 2018.333 2018.417 2018.500 2018.583 2018.667
2019 2019.000 2019.083 2019.167 2019.250 2019.333 2019.417 2019.500 2019.583 2019.667
2020 2020.000 2020.083 2020.167 2020.250 2020.333 2020.417
Oct Nov Dec
2002 2002.750 2002.833 2002.917
2003 2003.750 2003.833 2003.917
2004 2004.750 2004.833 2004.917
2005 2005.750 2005.833 2005.917
2006 2006.750 2006.833 2006.917
2007 2007.750 2007.833 2007.917
2008 2008.750 2008.833 2008.917
2009 2009.750 2009.833 2009.917
2010 2010.750 2010.833 2010.917
2011 2011.750 2011.833 2011.917
2012 2012.750 2012.833 2012.917
2013 2013.750 2013.833 2013.917
2014 2014.750 2014.833 2014.917
2015 2015.750 2015.833 2015.917
2016 2016.750 2016.833 2016.917
2017 2017.750 2017.833 2017.917
2018 2018.750 2018.833 2018.917
2019 2019.750 2019.833 2019.917
2020
##Here is the dput results
Also this is the result of dput of the time series
dput(NDVI_Chobe001.ts)
structure(c(0.258672185, 0.237639041, 0.223988035, 0.22397988,
0.28132144, 0.387710909, 0.556186453, 0.719580311, 0.443294248,
0.314433357, 0.292755672, 0.278023297, 0.246809774, 0.230477039,
0.228955071, 0.234966762, 0.396659718, 0.491330645, 0.716274496,
0.73417416, 0.576279481, 0.479403466, 0.423930923, 0.377550602,
0.313801774, 0.297335261, 0.27886131, 0.285054814, 0.323942137,
0.482912961, 0.544500134, 0.692623308, 0.512637643, 0.526592884,
0.432898755, 0.331892323, 0.294543398, 0.274633904, 0.247602217,
0.24889248, 0.268682448, 0.623132479, 0.732958587, 0.794572999,
0.697092229, 0.588587711, 0.389213056, 0.348485514, 0.282932917,
0.264831569, 0.254937301, 0.277647575, 0.342873991, 0.450392312,
0.631838679, 0.716638566, 0.508704542, 0.423229761, 0.324663042,
0.321186864, 0.295935102, 0.255618784, 0.257619908, 0.27254904,
0.270580191, 0.432220414, 0.585539863, 0.716581759, 0.593955107,
0.559614127, 0.388112159, 0.386833323, 0.323056858, 0.296201373,
0.299547175, 0.340499135, 0.487932489, 0.531619232, 0.755898976,
0.630418376, 0.624101053, 0.46624194, 0.407831925, 0.396696504,
0.348860597, 0.311130303, 0.301932775, 0.359536613, 0.389388896,
0.639178419, 0.693073002, 0.544238686, 0.608043068, 0.520223438,
0.489590537, 0.371915235, 0.322345492, 0.285747424, 0.262060636,
0.290601893, 0.272739968, 0.465184219, 0.597999142, 0.58280379,
0.498312536, 0.351555151, 0.313456794, 0.30176279, 0.272389062,
0.256200802, 0.257570355, 0.261360949, 0.280664516, 0.472871998,
0.635346196, 0.809469884, 0.708410897, 0.454619991, 0.374153554,
0.31216354, 0.277643235, 0.273912332, 0.279202729, 0.274954368,
0.3220379, 0.66542086, 0.786922135, 0.749038686, 0.494324261,
0.380828443, 0.31699487, 0.293321759, 0.275010923, 0.263032267,
0.254334753, 0.270928799, 0.319622211, 0.699834174, 0.733615599,
0.743950233, 0.701439492, 0.514223884, 0.414668945, 0.353937354,
0.304874948, 0.263697644, 0.25397833, 0.270116643, 0.310931558,
0.529621168, 0.769325391, 0.676328487, 0.561630209, 0.550257129,
0.408412794, 0.362071348, 0.294350827, 0.271100888, 0.26194204,
0.265314367, 0.311826075, 0.556399621, 0.586507353, 0.715697274,
0.71326184, 0.554275685, 0.423167172, 0.332214247, 0.316422341,
0.264796933, 0.253106015, 0.274547234, 0.274233387, 0.331729551,
0.568674796, 0.678751599, 0.651238604, 0.550351642, 0.50121297,
0.39598441, 0.373748125, 0.342521719, 0.308862893, 0.368914514,
0.346751371, 0.590197072, 0.550842239, 0.641805925, 0.73276961,
0.616976479, 0.501595155, 0.441702349, 0.420716154, 0.297677153,
0.302351869, 0.347572841, 0.347128221, 0.488411226, 0.739632011,
0.773688839, 0.527578039, 0.531276077, 0.481584383, 0.450086834,
0.331415825, 0.298545112, 0.281891087, 0.301013691, 0.334150401,
0.537372378, 0.756594273, 0.778707894, 0.728867792, 0.634829201,
0.415475576, 0.353712963), .Tsp = c(2002.5, 2020.41666666667,
12), class = "ts")
Here is my code
#Here I am Specifying the option parameters explicitly
opt=list() #Create an empty list to append individual model parameters
opt$period=12 #Period of the cyclic/seasonal component of the modis time series
opt$startTime=2002.500000 ##specify start time
Here I am plotting the data
out=beast(NDVI_Chobe001.ts, opt)
p<-plot(out)
However, the plot time series is in monthly frequency , see example here[enter image description here][1]
##Now I am extracting years from the time series to use when plotting
time <- as.yearmon( time(NDVI_Chobe001.ts))
time<-format.Date(x,"%Y")
plot(out,axis(1,time, labels = TRUE))
But this is not working, it is still displayed in monthly frequency, How do I lot my time series by year?
[1]: https://i.stack.imgur.com/VNxJC.jpg
What I tried is something like this:
Your time series data:
df <- structure(c(0.258672185, 0.237639041, 0.223988035, 0.22397988,
0.28132144, 0.387710909, 0.556186453, 0.719580311, 0.443294248,
0.314433357, 0.292755672, 0.278023297, 0.246809774, 0.230477039,
0.228955071, 0.234966762, 0.396659718, 0.491330645, 0.716274496,
0.73417416, 0.576279481, 0.479403466, 0.423930923, 0.377550602,
0.313801774, 0.297335261, 0.27886131, 0.285054814, 0.323942137,
0.482912961, 0.544500134, 0.692623308, 0.512637643, 0.526592884,
0.432898755, 0.331892323, 0.294543398, 0.274633904, 0.247602217,
0.24889248, 0.268682448, 0.623132479, 0.732958587, 0.794572999,
0.697092229, 0.588587711, 0.389213056, 0.348485514, 0.282932917,
0.264831569, 0.254937301, 0.277647575, 0.342873991, 0.450392312,
0.631838679, 0.716638566, 0.508704542, 0.423229761, 0.324663042,
0.321186864, 0.295935102, 0.255618784, 0.257619908, 0.27254904,
0.270580191, 0.432220414, 0.585539863, 0.716581759, 0.593955107,
0.559614127, 0.388112159, 0.386833323, 0.323056858, 0.296201373,
0.299547175, 0.340499135, 0.487932489, 0.531619232, 0.755898976,
0.630418376, 0.624101053, 0.46624194, 0.407831925, 0.396696504,
0.348860597, 0.311130303, 0.301932775, 0.359536613, 0.389388896,
0.639178419, 0.693073002, 0.544238686, 0.608043068, 0.520223438,
0.489590537, 0.371915235, 0.322345492, 0.285747424, 0.262060636,
0.290601893, 0.272739968, 0.465184219, 0.597999142, 0.58280379,
0.498312536, 0.351555151, 0.313456794, 0.30176279, 0.272389062,
0.256200802, 0.257570355, 0.261360949, 0.280664516, 0.472871998,
0.635346196, 0.809469884, 0.708410897, 0.454619991, 0.374153554,
0.31216354, 0.277643235, 0.273912332, 0.279202729, 0.274954368,
0.3220379, 0.66542086, 0.786922135, 0.749038686, 0.494324261,
0.380828443, 0.31699487, 0.293321759, 0.275010923, 0.263032267,
0.254334753, 0.270928799, 0.319622211, 0.699834174, 0.733615599,
0.743950233, 0.701439492, 0.514223884, 0.414668945, 0.353937354,
0.304874948, 0.263697644, 0.25397833, 0.270116643, 0.310931558,
0.529621168, 0.769325391, 0.676328487, 0.561630209, 0.550257129,
0.408412794, 0.362071348, 0.294350827, 0.271100888, 0.26194204,
0.265314367, 0.311826075, 0.556399621, 0.586507353, 0.715697274,
0.71326184, 0.554275685, 0.423167172, 0.332214247, 0.316422341,
0.264796933, 0.253106015, 0.274547234, 0.274233387, 0.331729551,
0.568674796, 0.678751599, 0.651238604, 0.550351642, 0.50121297,
0.39598441, 0.373748125, 0.342521719, 0.308862893, 0.368914514,
0.346751371, 0.590197072, 0.550842239, 0.641805925, 0.73276961,
0.616976479, 0.501595155, 0.441702349, 0.420716154, 0.297677153,
0.302351869, 0.347572841, 0.347128221, 0.488411226, 0.739632011,
0.773688839, 0.527578039, 0.531276077, 0.481584383, 0.450086834,
0.331415825, 0.298545112, 0.281891087, 0.301013691, 0.334150401,
0.537372378, 0.756594273, 0.778707894, 0.728867792, 0.634829201,
0.415475576, 0.353712963), .Tsp = c(2002.5, 2020.41666666667,
12), class = "ts")
Create the dataframe with proper dates:
# this will extract, convert and create a Date column from your data
df1 <- data.frame(as.matrix(df), date=time(df))
df1$year <- trunc(df1$date)
df1$month <- (df1$date - df1$year) * 12 + 1
df1$month <- as.integer(round(df1$month),0)
df1$Day <- 1
names(df1) <- c("Data", "Original_Date", "Year", "Month", "Day")
df1$Date <- paste(df1$Year, df1$Month, df1$Day, sep="-")
You can review, if all is good:
# check the structure
str(df1)
Finally, plot it:
# plot the data
library(ggplot2)
theme_set(theme_minimal())
ggplot(data = df1, aes(x = Date, y = Data)) + geom_line(color = "#00AFBB", size = 1)
A bit closer
plot(y=out$s, x=out$time, type='l') #The same as plot(out$s[,1]): plot the seasonal curve
plot(y=out$sProb, x=out$time, type='l') #Plot the probability of observing seasonal changepoints
plot(y=out$t, x=out$time, type='l') #The same as plot(out$t[,1]): plot the trend
plot(y=out$sProb, x=out$time, type='l') #Plot the probability of observing trend changepoints11
I know this question is old and my late reply may be irrelevant. Regardless, in the old version of Rbeast, if given a ts input, the beast function uses only its data vector not the time tag information (i.e., .tsp attributes such as start and frequency). In other words, the time info has to be additionally provided to beast; if not, the indices 1:N are used as default times. In the latest version (Rbeast v0.9.2), I changed the API a little bit; now beast can handle the time tag of ts objects.
Below is a test using your sample data for time series decomposition and changepoint detection.
ndvi= c( 0.258,0.237,0.223,0.223,0.281,0.387,0.556,0.719,0.443,0.314,0.292,0.278,0.246,0.230,0.228,0.234,0.396,0.491,0.716, 0.734,0.576,0.479,0.423,0.377,0.313,0.297,0.278,0.285,0.323,0.482,0.544,0.692,0.512,0.526,0.432,0.331,0.294,0.274,
0.247,0.248,0.268,0.623,0.732,0.794,0.697,0.588,0.389,0.348,0.282,0.264,0.254,0.277,0.342,0.450,0.631,0.716,0.508, 0.423,0.324,0.321,0.295,0.255,0.257,0.272,0.270,0.432,0.585,0.716,0.593,0.559,0.388,0.386,0.323,0.296,0.299,0.340,
0.487,0.531,0.755,0.630,0.624,0.466,0.407,0.396,0.348,0.311,0.301,0.359,0.389,0.639,0.693,0.544,0.608,0.520,0.489, 0.371,0.322,0.285,0.262,0.290,0.272,0.465,0.597,0.582,0.498,0.351,0.313,0.301,0.272,0.256,0.257,0.261,0.280,0.472,
0.635,0.809,0.708,0.454,0.374,0.312,0.277,0.273,0.279,0.274,0.322,0.665,0.786,0.749,0.494,0.380,0.316,0.293,0.275, 0.263,0.254,0.270,0.319,0.699,0.733,0.743,0.701,0.514,0.414,0.353,0.304,0.263,0.253,0.270,0.310,0.529,0.769,0.676,
0.561,0.550,0.408,0.362,0.294,0.271,0.261,0.265,0.311,0.556,0.586,0.715,0.713,0.554,0.423,0.332,0.316,0.264,0.253, 0.274,0.274,0.331,0.568,0.678,0.651,0.550,0.501,0.395,0.373,0.342,0.308,0.368,0.346,0.590,0.550,0.641,0.732,0.616,
0.501,0.441,0.420,0.297,0.302,0.347,0.347,0.488,0.739,0.773,0.527,0.531,0.481,0.450,0.331,0.298,0.281,0.301,0.334, 0.537,0.756,0.778,0.728,0.634,0.415,0.353)
ndvi is a data vector; create a ts object out of it as the input to beast:
ndvi_ts = ts(ndvi, start=2002.5, frequency = 12)
out = beast(ndvi_ts)
plot(out)
# By default, seasonality is fitted as a harmonic curve; use SVD-based bases instead
out = beast(ndvi_ts, season='svd')
plot(out)
# If the input is a pure data vector, the time info has to be specified
# via 'start', 'deltat' (delta T), and 'freq'; otherwise, default times
# (i.e., 1:216) would be used.
out = beast( ndvi, start=2002.5, deltat=1/12, freq=12, season='svd')
plot(out)
The beast output is a LIST object of class 'beast' and can't be directly plotted with ggplot2. The plot function above is not R's own function but Rbeast's implementation of the S3 method 'plot.beast'. If customary plots are needed, individual variables of the output can be extracted for use in R's base plot or ggplot2. Below is an example:
out=beast(ndvi_ts, season='svd')
# out is a LIST variable; extract some elements
t = out$time # time
y = out$trend$Y # fitted trend compnt
yci = out$trend$CI[,,1] # estimated 95% CI interval for trend
cp = out$trend$cp # most probable changepoints in trend
ncp = sum(!is.nan(cp)) # number of changepoints given in cp
ymax=max(yci)
ymin=min(yci)
plot( x=c(min(t),max(t)), y=c(ymin,ymax), type='n', xlab='time', ylab='trend' )
lines(t,y)
polygon( x=c(t,rev(t)), y=c( yci[,1], rev(yci[,2]) ) , col=rgb(0,1,0,.2),border=NA )
yrange=par('usr')[3:4]
for (i in 1:ncp) {
lines( c(cp[i], cp[i] ), y=yrange, type='l', lty='dashed' )
}
If multiple subplots are needed, one way is to use R's par(new=TRUE) option, as shown below.
t = out$time # time
y = out$trend$Y # fitted trend compnt
yci = out$trend$CI[,,1] # estimated 95% CI interval for trend
cp = out$trend$cp # most probable changepoints in trend
ncp = sum(!is.nan(cp)) # number of changepoints given in cp
prob = out$trend$cpOccPr # probability of changepoints occurrence over time
ymax=max(yci)
ymin=min(yci)
plot.new()
# FIRST PLOT: Fine-tune the four margin numbers in plt to re-position
par(plt = c(0.15, 1-0.01, 1-0.01-.5, 1-0.01-.5+0.4),new=TRUE)
plot( x=c(min(t),max(t)), y=c(ymin,ymax), type='n', xaxt='n', xlab=NA, ylab='trend')
lines( t, y )
polygon( x=c( t, rev(t) ), y=c( yci[,1], rev(yci[,2]) ) , col=rgb(0,1,0,.2),border=NA )
yrange=par('usr')[3:4]
for (i in 1:ncp) {
lines( c(cp[i], cp[i] ), y=yrange, type='l', lty='dashed' )
}
# SECOND PLOT: Fine-tune the four margin numbers in plt to re-position
par(plt = c(0.15, 1-0.01, 1-0.01-.5-0.3, 1-0.01-.5-0.3+0.3),new=TRUE)
plot( x=t, y=prob, type='l', xlab='time', ylab='Prob' )
Not sure how useful this is. In any case, thanks for testing out Rbeast. If new features are desired, pls let me know.

R tmap tm_facet assigns values to the wrong location

I'm trying to use tm_facets to display data (in this case on maize yields) in 2005, 2050, and 2080. The test.RDS file is available here.
library(tmap)
map.temp <- readRDS("test.RDS")
title <- "Maize rainfed yield <br> (mt/ha)"
legend_title <- "(mt/ha)"
breaks <- c(1.0, 2139.2, 4277.5, 6415.8, 8554)
tm_shape(map.temp) +
tm_polygons(col = "value", title = legend_title) +
tm_facets(by = "year") +
tm_layout(main.title = title) +
tm_view(view.legend.position = c("left", "bottom"))
The code above does this, but displays the data in the wrong polygon and wrong years. To see this, run the script and click the dark red area in northeast Canada. The popup in all three maps says AMR_RUS with value of 5,634, but the colors are different. View the map.temp file (I'm using Rstudio to do all of this). Filter on FPU with AMR_RUS. The 2005 value is 6,047, 2050 is 5634 and 2080 is 4406 (climate change will reduce yields in this area). Next look at the first couple of entries in the geometry column. The lat long coordinates are for a region along the Chinese-Russian border. The Amur River makes up that border and AMR_RUS FPU (food production unit) is to the north of the Amur River in Russia.
Is the problem with my code or data or the tm_facet function in tmap?
Unfortunately, I can't figure out a solution with tmap, and not sure why is doing that misplacing of polygon names and values in the popup. UPDATE: seems that this was a tmap bug, which was immediately fixed - see tmap issue 268.
I know you asked for tmap solution, but, alternatively, could be worth exploring a solution with mapview - check this out and see if it works for you:
library(mapview)
breaks <- c(1.0, 2139.2, 4277.5, 6415.8, 8554)
m_2005 <- mapview(map.temp[map.temp$year == 2005, ],
zcol = "value",
at = breaks,
layer.name = "2005 - mt/ha")
m_2050 <- mapview(map.temp[map.temp$year == 2050, ],
zcol = "value",
at = breaks,
layer.name = "2050 - mt/ha")
m_2080 <- mapview(map.temp[map.temp$year == 2080, ],
zcol = "value",
at = breaks,
layer.name = "2080 - mt/ha")
sync(m_2005, m_2050, m_2080) # add ncol = 1, if you wish 1 column representation

"BAD_COLUMN_NAME" message from Bokeh plot

I am trying to create a simple bokeh chart (vert or hor) from a csv file and seem to be having issues. I am able to create the chart utilizing ColumnDataSource by listing items manually, but when I try to create the same chart from a simple csv file, it seems to give me trouble. Now I am trying to just create the figure by reading a pandas df. I am getting the dreaded Bad Column Name error message. Any help is appreciated. First time posting so let me know if I have posted incorrectly and I will fix. Thanks in advance.
from bokeh.io import output_notebook, show
output_notebook()
from bokeh.core.properties import value
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource,
HoverTool, FactorRange, Range1d
from bokeh.plotting import figure
from bokeh.transform import dodge
output_file("test.html")
import pandas as pd
df = pd.read_csv(r'C:\test.csv')
print(df)
Mains Total Length Length Surveyed
0 1.0 88.4 87.6
1 2.0 313.8 316.8
2 3.0 271.0 265.6
3 4.0 155.0 153.1
4 5.0 301.8 299.0
5 6.0 293.9 132.3
6 7.0 148.1 147.2
7 8.0 292.9 290.1
8 9.0 307.6 306.0
9 10.0 559.0 236.0
10 11.0 448.8 441.5
11 12.0 297.9 13.0
12 13.0 172.2 67.5
source = ColumnDataSource(data=dict(df))
Mains = data=dict(df)
data = {'Mains': df}
p = figure(x_range=(0,20), y_range=(0, 500),
plot_height=250, title="CCTV Survey August 6-
9th,
2018",
toolbar_location=None, tools="")
p.vbar(x=dodge('Mains', -0.25, range=p.x_range),
top='2015', width=0.2, source=source,
color="#c9d9d3", legend=value("Total
Length"))
p.vbar(x=dodge('Mains', 0.0, range=p.x_range),
top='2016', width=0.2, source=source,
color="#718dbf", legend=value("Length
Surveyed"))
p.add_tools(HoverTool(tooltips=[("Total Length",
"#2015 ft"),("Length Surveyed", "#2016
ft")]))
p.xaxis.major_label_orientation = 1.4
##p.x_range.factors=data_dict['x']
##p.x_range.range_padding = 0.0
p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"
show(p)
ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name: 2015 [renderer: GlyphRenderer(id='107d32b5-2700-4608-b1d8-9d0602f82a5b', ...)]
ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name: 2016 [renderer: GlyphRenderer(id='4dcb960b-b29e-4998-972a-046311d037f8', ...)]
You are telling Bokeh that the top of the bars should be driven by a column named "2016":
top='2016', width=0.2, source=source,
But your dataframe / column data source has no such column. You need to configure the bar glyphs to use columns that are actually part of your data (presumably
"Total Length" or "Length Surveyed")

Why does tmap render 80 times faster than ggplot2? [Plotting shapefiles in R with ggplot2::geom_sf(), using XQuartz/X11 graphics device on macOS]

Update/Edit/Reprex: Rendering the same spatial data with the same graphics device takes 1 second with tmap versus 80 seconds with ggplot2, even though the tmap plot's R object is 80x larger in size. Difference in internals and/or implementation btw. packages & graphics device?
library(ggplot2); library(sf);
library(tmap); library(tidyverse)
library(here) # project directory
data(World) # sf object from tmap; Provides Africa polygon
# 'd' data pulled from acleddata.com/data, restricted to Aug 18 2017 - Aug 18 2018, Region: N/S/E/W/Middle Africa only
d <- read.csv(here('2017-08-18-2018-08-18-Eastern_Africa-Middle_Africa-Northern_Africa-Southern_Africa-Western_Africa.csv'))
dsf <- st_as_sf(d, coords = c('longitude', 'latitude'), crs = 4326)
Data used:
'World' shapefile data from the tmap package itself, and
[acleddata.com/data][1], ACLED conflict events restricted to Africa between August 18 2017 and August 18 2018 (7.8 MB .csv; these filters:)
[![enter image description here][2]][2]
Plot rendering:
# ggplot2; build plot, assign to object
dev.cur() # RStudioGD on macOS: quartz
system.time(p <- ggplot(World %>% filter(continent == 'Africa')) +
geom_sf() +
geom_sf(data = dsf, aes(fill = event_type,
color = event_type)) +
ggthemes::theme_tufte() +
theme(legend.key.size = unit(.1, 'cm'),
legend.title = element_blank()))
# user system elapsed
# 0.016 0.001 0.017
object.size(p)
# 175312 bytes
# render
system.time(print(p))
# user system elapsed
# 84.755 0.432 85.418 # Note over 80 seconds
[![ggplot2 png][3]][3]
# tmap; build plot, assign to object
tmap_mode('plot')
system.time(tm <- tm_shape(World, filter =
(World$continent == 'Africa')) +
tm_polygons(group = 'Countries') +
tm_shape(dsf) +
tm_dots(col = 'event_type', group = 'event_type'))
# user system elapsed
# 0.000 0.000 0.001
object.size(tm)
# 14331968 bytes # This is 80x ggplot2 plot's object size
# 14331968/175312 = 81.75121
# render
dev.cur() # RStudioGD on macOS: quartz
system.time(print(tm))
# user system elapsed
# 1.438 0.038 1.484 # Note 1 second
[![tmap png][4]][4]
[Previous inquiry into geom_sf() & graphics devices, without the tmap comparison:]
TL;DR:
I am trying to speed up my plotting speed by switching graphics devices to X11, since my default Quartz graphics device is slow. After downloading XQuartz (to connect to the X11 graphics device) and calling grDevices::X11(), I don't understand the errors I'm getting.
X11(type = "cairo")
# Error in .External2(C_X11, d$display, d$width, d$height, d$pointsize, :
# unable to start device X11
# In addition: Warning message:
# In X11() : unable to open connection to X11 display 'cairo'
#> Warning in X11(type = "cairo"): unable to open connection to X11 display ''
And when I call R from a XQuartz.app terminal on macOS instead, the error message is slightly different:
X11(type = "cairo")
#> Error in .External2(C_X11, d$display, d$width, d$height, d$pointsize, : unable to start device X11cairo
End TL;DR
Broader Context:
Plotting large shapefiles with `ggplot2::geom_sf()`, the quartz graphics device used in macOS plots considerably slower than other devices, and while this larger performance issue is being resolved, I want to change my device from Quartz to X11.
I downloaded XQuartz, following advice from the [RStudio forums][5], but my code doesn't successfully call X11, even when I launch R from XQuartz.
Proof, using the same data as the RStudio forum poster:
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
library(ggplot2)
tmpzip <- tempfile(fileext = ".zip")
download.file("https://github.com/bcgov/bcmaps.rdata/blob/master/data-raw/ecoregions/ecoregions.zip?raw=true", destfile = tmpzip)
gdb_path <- unzip(tmpzip, exdir = tempdir())
ecoregions <- sf::read_sf(dirname(gdb_path[1]))
## Create ggplot2 object
ecoregions_gg <- ggplot() + geom_sf(data = ecoregions)
# Running quartz device - default macOS
system.time(print(ecoregions_gg))
#> user system elapsed
#> 128.980 0.774 130.375
### ^ Note two full minutes!
[![Shapefile][6]][6]
This default device runs for an unusually long 129 seconds given the size.
X11 should run faster according to the RStudio forum. A test on a (granted, faster) Windows 7 machine (32 GB RAM, 3.60 GHz) using its default graphics device (not Quartz), yielded:
#> user system elapsed
#> 2.16 2.24 4.46
### ^Only two seconds
While people are troubleshooting the general geom_sf / Quartz performance problems ([Github Issue 1][7], [Github Issue 2][8]), how can I use my XQuartz install to run X11 and speed up my shapefile plotting?
[1]: http://acleddata.com/data
[2]: https://i.stack.imgur.com/iEA9j.png
[3]: https://i.stack.imgur.com/BCYzl.png
[4]: https://i.stack.imgur.com/5lgZB.png
[5]: https://community.rstudio.com/t/ggplot2-geom-sf-performance/3251
[6]: https://i.stack.imgur.com/ILhQP.png
[7]: https://github.com/tidyverse/ggplot2/issues/2655
[8]: https://github.com/tidyverse/ggplot2/issues/2718

Can't move timePlot legend when using R openair

I cannot seem to move the legend from the bottom of my graph when using timePlot from R openair. Can anyone assist?
Here is my script and some data:
library(openair)
timeozone <- import(npsfiles[2], date="year", date.format = "%Y", header=TRUE, na.strings="NA")
timePlot(timeozone, pollutant = c("BIBEKB", "MACAGO", "MACAHM", "VOYASB"), group = TRUE, ylab = "Ozone (ppbv)", key.columns = 1)
Data:
Year BIBEKB
1990 70.25
1991 59.25
1992 62.5
1993 64.25
1994 76
1995 68.25
1996 78.375
1997 66.25
I have tried:
auto.key = list(space = "right")
space = "right"
key.position = "right"
I have also tried putting the legend specification outside the timePlot line of code, but I am hoping to be able to get it to work with the location specification in the timePlot function. Thanks for any help you can provide:
plot.new()
legend(1, -1, c("BIBEKB","MACAGO","MACAHM", "VOYASB"))
I saw that "key.position =" function option was added in github repo of openair timePlot
But it's developer version. So you can try and install it manually. May be it'll help you!
How to install from Github repository

Resources