Can't move timePlot legend when using R openair - r

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

Related

ggplot2 black bar when outputting plot using bbplot theme

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.

Barplot form data in a dataframe

I have the following data in a data frame;
TYPE_OF_COMPANY COUNT_OF_COMPANIES
AIM-Listed 876
Charitable-organisation 82
Industrial-Provident 50
Limited-Partnership 2
Limited by Guarantee 277
Limited Liability Partnership 167
Listed-LSE 1131
Not-Companies-Act 75
Private Limited Company 1163
Public-Unlisted 418
Royal-Charter 5
Unlimited 111
I want represent this data is a bar plot. When this data is in a table then my code succeeds but I was having a problem sorting the data but I was happy with the out put using the following code:
barplot(counts,
Xlab='TYPES_OF_COMPANIES', ylab='TYPE_OF_COMPANY', ylim=c(0,1200),
names.arg = c("LP", "RC", "IP",
"N-C-A", "C-O", "Guarantee",
"U-L", "C-O",
"LLP","L-G","P-U",
"A-L", "LSE", "PL"),
main='Number of Different Types of Companies in the database')
When I tried to modify the code to use a data frame, it gave me an error. I know I could use ggplot2 package to do it, but this is for illustration, and I want to do it in base R.
You can show me how to sort the table or how to make barplot work with the data frame.
Any help is greatly appreciated.
Does this work for you?
barplot(sort(counts$COUNT_OF_COMPANIES),
xlab = 'TYPES_OF_COMPANIES', ylab = 'Count', ylim = c(0, 1200),
names.arg = c("AIM", "Charity", "I-P", "L-P",
"L-G", "LLP", "L-LSE", "NCA",
"PLC", "P-UL", "RC", "UL")[order(counts$COUNT_OF_COMPANIES)],
main = 'Number of Different Types of Companies in the database')
Instead of writing the names.arg, You could do:
labs <- c("AIM", "Charity", "I-P", "L-P", "L-G", "LLP",
"L-LSE", "NCA", "PLC", "P-UL", "RC", "UL")
new_df <- transform(df, labels = ordered(labs, labs[order(COUNT_OF_COMPANIES)]))
barplot(COUNT_OF_COMPANIES~labels, new_df, xlab='TYPES_OF_COMPANIES',
ylab='TYPE_OF_COMPANY',
main='Number of Different Types of Companies in the database')

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

R: Finding the location of tick marks when plotting dates

I'm running an R script which plots data over time using plot and some data taken as.Date. I am then adding using grid to add guidelines like so:
plot(as.Date(data[["Date"]]), -data[["dbns"]], lwd=1, col = colours[i], type="l",ylim=ylimit, xlim=xlimit, xlab="", ylab =ylabel)
grid(NULL,NULL,lwd=1)
Below is an example of how this comes out (actually example is after this plot is looped over 3 times to add each line)
The problem I am having is that the guidelines added with grid do not line up with the default tick marks added by plot. The plot function by default adds tick every 10 years, which is what I want. Looking at axTicks(1) gives me 0 5000 10000 15000 which is the locations of guidelines in units of days after 1/1/1970.
Is there a way to find out the location of the tickmarks that have been added? And more importantly, is there then a way to set the guidelines to match these locations?
I realise that for this example the ticks are [0, 365.25*10, 365.25*20 etc.] but an answer which solves a general case would be nice.
Thanks
JP
EDIT:
Here is a reproducible example to show exactly what I mean.
data <- read.csv("test.csv"), header =TRUE)
data[["Date"]] <- as.Date(data[["Date"]], format = "%d/%m/%Y")
data<-data[order(as.Date(data$Date)),]
plot(as.Date(data[["Date"]]), -data[["dbns"]], type="l", xlim=xlimit)
grid(NULL,NULL,lwd=3)
Where the file test.csv looks like this (extract of actual data)
bore Date dbns
42230210A 20/01/2009 13.13
42230210A 8/05/2009 13.21
42230210A 18/06/2009 13.19
42230210A 3/08/2009 13.2
42230210A 2/09/2009 13.25
42230210A 1/10/2009 13.3
42230210A 3/12/2009 13.32
42230210A 24/02/2010 13.3
42230210A 18/05/2010 13.3
42230210A 25/04/2012 11.45
42230210A 27/09/1966 11.18
42230210A 28/10/1969 11.14
42230210A 6/01/1970 11.03
42230210A 5/06/1973 10.68
42230210A 28/08/1973 10.63
42230210A 2/10/1973 10.73
42230210A 4/12/1973 10.7
42230210A 20/02/1980 11.39
42230210A 29/04/1980 11.45
42230210A 27/08/1980 11.45
42230210A 22/06/1988 11.14
42230210A 27/02/1996 11.78
42230210A 6/08/1996 11.68
42230210A 8/02/2000 11.64
42230210A 8/06/2000 11.71
42230210A 7/09/2000 11.75
42230210A 15/07/2008 13.01
The output of which is below. The guidelines are offset from the tick marks?
So checking ?grid suggests that what's going on is that it is placing the grid lines based on the "default" tick mark locations, but since you plotted a date object, R didn't use the "default" (in the sense of treating the date as a numeric variable) to place the tick marks.
The documentation further notes that if you need finer control, you should use abline to place them directly.
So you probably want to do this:
plot(as.Date(data[["Date"]]), -data[["dbns"]], type="l")
grid(NA,NULL,lwd=3)
v <- as.numeric(as.Date(paste0(c(1970,1980,1990,2000,2010),'-01-01')))
abline(v = v,lty = "dotted",col = "lightgray",lwd = 3)
There is a function Axis.Date which creates appropriately pretty axis tick locations for an axis for a Date object. This does not change par('xaxp') which grid uses to find the locations of the tick marks.
We can get around by creating the axis manually (and saving the values)
# assuming your data is called DD
DD$Date <- as.Date(as.character(DD$Date), format = '%d/%m/%Y')
# don't plot the x axis (xaxt = 'n')
plot(-dbns~Date,data = DD[order(DD$Date),], type="l", xaxt = 'n')
# create the axis and save the tick locations
at <- as.numeric(Axis(side = 1, x = DD$Date))
grid(NA,NULL,lwd=3)
abline(v = at,lty = "dotted",col = "lightgray",lwd = 3)
The answer to your questions are the following ones:
Is there a way to find out the location of the tickmarks that have been added?
Just after the plot function call, use the function axis.Date
x_ticks <- axis.Date(1, data$Date, labels = FALSE)
Variable x_ticks contains the location of the ticks used in the x axis (identified by 1 in the first parameter of the function).
And more importantly, is there then a way to set the guidelines to match these locations?
grid(NA, NULL, lwd=3)
x_ticks <- axis.Date(1, data$Date, labels = FALSE)
abline(v = x_ticks, lty = 3, col ="lightgray", lwd = 3)
With the NA in the first parameter of the grid function (nx = NA), you avoid that the vertical guidelines are printed in the wrong places. The second gives you the location of the ticks. Finally, the third prints the guidelines in the right locations.
In this way, the plot you obtain looks like, I guess, the one you expected

map with wrong colors but right assigned on legend?

I've been mapping a choropleth map and when I compare the colours drawn to the numbers they re assigned to it doesn't fit.
so here is my data
zip latitude longitude count2.x count2.freq reg colorBuckets colma
99501 61.21680 -149.87828 AK 67 Alaska 1 #EDF8FB
35010 32.90343 -85.92669 AL 1582 Alabama 3 #99D8C9
90001 33.97291 -118.24878 CA 20970 California 6 #006D2C
20001 38.90771 -77.01732 DC 952 NA 2 #CCECE6
so the code from the beginning that I've been using is the following
library("zipcode")
library("maps")
library("plyr")
library("RColorBrewer")
colors=brewer.pal(6, "BuGn")
data(zipcode)
merge1<-merge(zipcode, tel2, by.x='zip', by.y='zip_code', all.y=TRUE)
result<- ddply(merge1, .(state), transform, count2 = count(state))
#remove NA's#
final<- result[complete.cases(result),]
#remove duplicates#
nodupl <- final[!duplicated(final$state),]
#add state to abbreviations#
nodupl$reg<-state.name[match(nodupl$count2.x, state.abb)]
#intervalle bestimmen#
nodupl$colorBuckets<- as.numeric(cut(nodupl$count2.freq, c(0,500,1000,5000,10000,15000,22000)))
#intervall legend#
text<- c("< 500", "500 - 999", "1000 - 4999","5000 - 9999", "10000 - 14999", "15000 - 22000")
#see what color is assign to where#
nodupl$colma<- colors[nodupl$colorBuckets]
map("state",regions=nodupl$reg, exact=FALSE, col = colors[nodupl$colorBuckets], fill = TRUE,resolution = 0,lty = 0)
map("state",col = "black",fill=FALSE,add=TRUE,lty=1,lwd=1)
#legend plotten#
par(xpd=TRUE)
legend("bottomleft", text, horiz=FALSE, fill=colors)
so the problem is again if I see the legend the colours are assigned right but if I double check the numbers in my column (row count2.freq) with the colours on the map they don't fit. e.g california is really light colored but it should be dark. does anyone see what has been done wrong. plus I have some trouble with positioning the legend the way I have it is right on the map so Idon't see the map anymore. what could I do about that?
thanks for your help even though its saturday
The region Alaska is not available in the map function. Hence, your map shows the second and third entry of nodupl$reg (i.e., Alabama and California). But the first and second of your colours are used.
To print these states in the desired colours, use the command
map("state", regions=nodupl$reg[2:3], exact=FALSE,
col = colors[nodupl$colorBuckets][2:3], fill = TRUE,resolution = 0,lty = 0)
But I recommend searching a map of Alaska too.

Resources