Getting plot errors when creating custom axes - r

I have an issue with the creation of custom axes in the base plotting system in R, I have the following data frame for which I want to plot a trend to show the changes for each year:
year <- c(2000, 2002, 2005, 2009)
values <- c(7332967, 5332780, 5135760, 3464206)
x <- data.frame(year, values)
## year values
## 1 2000 733296
## 2 2002 533278
## 3 2005 513576
## 4 2009 346420
My first attempt is:
plot(x$year, x$value,
xlab = "Year",
ylab = "Value",
type = "b")
However, that gives me a skewed x and y axis for the four values I have in the data frame. I would like for the x axis to only contain the four values under the "year" column and y axis to only contain the four values under the "values" column.
For this purpose I tried to create custom x and y axis but that resulted in errors:
plot(x$year, x$value,
type = "b",
xaxt = "n",
yaxt = "n",
xlab = "Year",
ylab = "Values",
axis(1, at = 1:nrow(x), labels = x$year),
axis(2, at = 1:nrow(x), labels = x$value))
"Error in plot.window(...) : invalid 'xlim' value"
and:
plot(x$year, x$value,
type = "b",
xaxt = "n",
yaxt = "n",
xlab = "Year",
ylab = "Values",
axis(1, at = 1:nrow(x), labels = x$year),
axis(2, at = 1:nrow(x), labels = x$value),
xlim = c(min(data_plot$year), max(data_plot$year)),
ylim = c(min(data_plot$Emissions), max(data_plot$Emissions)))
"Error in strsplit(log, NULL) : non-character argument"
I am quite new to R and tried searching for solutions on various sites, however, nothing seems to solve the issue so any help provided would be much appreciated.

axis is a separate function, not an argument to plot, so try the following:
# First make some extra space on the left for the long numeric axis labels
par(mar=c(5, 6, 1, 1))
# Now plot the points, but suppress the axes
plot(x$year, x$values, xaxt='n', yaxt='n', xlab='Year', ylab='', type='b')
# Add the axes
axis(1, at=x$year, labels=x$year, cex.axis=0.8)
axis(2, at=x$values, labels=x$values, las=1, cex.axis=0.8)
# Add the y label a bit further away from the axis
title(ylab='Value', line=4)

Related

How can I change the name of my x value in r plot

I have the plot:
plot(Combined$TIMESTAMP, # Draw first time series
Combined$GPP_NT_VUT_REF,
type = "l",
col = 2,
ylim = c(0, 15),
xlab = "",
ylab = expression(paste("GPP [gC m"^"-2 "," day "^"-1]"))) +
lines(Combined$TIMESTAMP, # Draw second time series
Combined$GPP_WRF_mean,
type = "l",
col = 3) +
legend("topright", # Add legend to plot
c("OBS", "WRF"),
lty = 1,
col = 2:4)
which produces the timeseries graph with the name of x values equal to gen, feb, mar and I want to convert my x values in J,F,M,A,M,J...
I tried with:
axis(1, at=1:12, labels=month.name, cex.axis=0.5)
but it doesn't work - any help?
Keep the first letter of month.name or from month.abb:
month.1st.letter <- sub("(^[[:upper:]]).*$", "\\1", month.abb)
Then use this vector as the labels argument.
axis(1, at = 1:12, labels = month.1st.letter, cex.axis = 0.5)
Edit
Start by removing the plus signs from the code, base R graphics do not add up. Then, in the plot instruction include xaxt = "n". And plot the axis right after it.
plot(Combined$TIMESTAMP, # Draw first time series
Combined$GPP_NT_VUT_REF,
type = "l",
col = 2,
ylim = c(0, 15),
xaxt = "n", # here, no x axis
xlab = "",
ylab = expression(paste("GPP [gC m"^"-2 "," day "^"-1]")))
axis(1, at = 1:12, labels = month.1st.letter, cex.axis = 0.5)

How can I get to print ALL of the labels in the x axis in R?

Please understand that this is not a serious exercise or any research project, and I would ask the context be left to the side as ane
The issue is that when I try to plot the values on a column against the names of the countries, some of them are excluded from the x axis, and I don't know why.
Here is the data.
And this is the code:
require(RCurl)
require(foreign)
require(tidyverse)
x = getURL("https://raw.githubusercontent.com/RInterested/PLOTS/master/drinks_csv.csv")
data <- read.csv(textConnection(x))
data <- data[,c(1:5,8)]
plot(data$country,data$cases,las=2, xlab="", ylab="")
How do I either print different alternate countries, or all of them in the x axis?
Well, there are 169 countries, so they'd have to be pretty small to print all of them.
plot(data$country,data$cases,las=2, xlab="", ylab="", xaxt = 'n')
axis(1, at = 1:length(data$country), labels = data$country, cex.axis = 0.1, las = 2)
We can select which countries to plot x-axis ticks for by finding their indices in the rows of data$country and then using axis to plot those selected countries.
my.countries <- match(c("poland","japan","togo", "belarus"),data$country)
plot(data$country,data$cases,las=2, xlab="", ylab="", xaxt = 'n')
axis(1, at = my.countries, labels = data$country[my.countries], las = 2)

How to label only specific ticks in a base R plot?

How to label only specific ticks in a base R plot? In the following example I would like to plot ten ticks, but only the first and the last one should be labelled.
plot(1 : 10, xaxt = "n", xlab = 'Some Letters')
axis(1, at = c(1, 10), labels = letters[1 : 2])

R barplots: specify intervals of date-based x-axis

I've been producing different sets of charts, all in R base. I have a problem though with barplots. I've formatted the x-axis to show the dates by year, however, many years show up several times. I would like each year to only show up once.
Here's my example code:
library(quantmod)
start <- as.Date("01/01/2010", "%d/%m/%Y")
#Download FRED data
tickers <- c("WTISPLC", "DCOILBRENTEU")
fred <- lapply(tickers, function(sym) {na.omit(getSymbols(sym, src="FRED", auto.assign=FALSE, return.class = "zoo"))})
df <- do.call(merge, fred)
#Subset for start date
df <- subset(df, index(df)>=start)
#Create bar plot
par(mar = c(5,5,5,5))
barplot(df[,2], names.arg=format(index(df), "%Y"), ann=FALSE, bty="n", tck=-0, col=1:1, border=NA, space=0); title(main="Example chart", ylab="y-axis")
This example should be reproducible and show clearly what I mean. Now, I've been researching how to add a separate x-axis and how to define that axis. So, I've tried to add the following code:
#Plot bars but without x-axis
barplot(df[,2], names.arg=format(index(df), "%Y"), ann=FALSE, bty="n", tck=-0, xaxt="n", col=1:1, border=NA, space=0); title(main="Example chart", ylab="y-axis")
# Set x-axis parameters
x_min <- min(index(df))
x_max <- max(index(df))
xf="%Y"
#Add x-axis
axis.Date(1, at=seq(as.Date(x_min), x_max, "years"), format=xf, las=1, tck=-0)
This does not give me an error message, but it also does absolutely nothing in terms of drawing an x-axis.
Please do not provide a solution for ggplot. Even though I like ggplot, these barplots are part of a bigger project for me, all using R base and I would not like to introduce ggplot into this project now.
Thanks!
If you are not limited to barplot, you may use the following very simple solution using plot.zoo behind the screens:
# only use what you want, and avoid multiple plots
df2 <- df[ , 2]
# use zoo.plot's functionality
plot(df2, main = "Example Chart", ylab = "y-axis", xlab = "")
This yields the following plot:
I know it is not a barplot, but I don't see what a barplot would add here. Please let me know, whether this is what you want or not.
Edit 1
If you do want to use barplot you may use the following code:
### get index of ts in year format
index_y <- format(index(df), "%Y")
### logical vector with true if it is the start of a new year
index_u <- !duplicated(index_y)
### index of start of new year for tick marks
at_tick <- which(index_u)
### label of start of new year
labels <- index_y[index_u]
### draw barplot without X-axis, and store in bp
### bp (bar midpoints) is used to set the ticks right with the axis function
bp <- barplot(df[,2], xaxt = "n", ylab= "y-axis")
axis(side = 1, at = bp[at_tick] , labels = labels)
yielding the following plot:
Please let me know, whether this is what you want.
Edit 2
We need to take into account two bits of information, when explaining why the ticks and labels group together at the left-hand side.
(1) in barplot, space defines the amount of space before each bar (as a fraction of the average bar width). In our case, it defaults to around zero (see ?barplot for details). In the illustration below, we use spaces of 0.0, 0.5, and 2.0
(2) Barplot returns a numeric vector with the midpoints of the bars drawn (again see the help pages for more detailed info). We can use these midpoints to add information to the graph, like we do in the following excerpt: after storing the result of barplot in bp, we use bp to set the ticks: axis(... at = bp[at_tick] ... ).
When we add space, the location of the bar midpoints change. So, when we want to use the bar midpoints after adding space, we need to be sure we have the right information. Simply stated, use the vector returned by barplot with the call where you added space. If you don't, the graph will be messed up. In the below, if you continue to use the bar-midpoints of the call with (space=0), and you increase space, the ticks and labels will group at the left-hand side.
Below, I illustrate this with your data limited to 3 months in 2017.
In the top layer 3 barplots are drawn with space equal to 0.0, 0.5 and 2.0. The information used to calculated the location of ticks and labels is recalculated and saved at every plot.
In the bottom layer, the same 3 barplots are drawn, but the information used to draw the ticks and labels is only created with the first plot (space=0.0)
# Subset for NEW start for illustration of space and bp
start2 <- as.Date("01/10/2017", "%d/%m/%Y")
df2 <- subset(df, index(df)>=start2)
### get index of ts in month format, define ticks and labels
index_y2 <- format(index(df2), "%m")
at_tick2 <- which(!duplicated(index_y2))
labels2 <- index_y2[!duplicated(index_y2)]
par(mfrow = c(2,3))
bp2 <- barplot(df2[,2], xaxt = "n", ylab= "y-axis", space= 0.0, main ="Space = 0.0")
axis(side = 1, at = bp2[at_tick2] , labels = labels2)
bp2 <- barplot(df2[,2], xaxt = "n", ylab= "y-axis", space= 0.5, main ="Space = 0.5")
axis(side = 1, at = bp2[at_tick2] , labels = labels2)
bp2 <- barplot(df2[,2], xaxt = "n", ylab= "y-axis", space= 2.0, main ="Space = 2.0")
axis(side = 1, at = bp2[at_tick2] , labels = labels2)
### the lower layer
bp2 <- barplot(df2[,2], xaxt = "n", ylab= "y-axis", space= 0.0, main ="Space = 0.0")
axis(side = 1, at = bp2[at_tick2] , labels = labels2)
barplot(df2[,2], xaxt = "n", ylab= "y-axis", space= 0.5, main ="Space = 0.5")
axis(side = 1, at = bp2[at_tick2] , labels = labels2)
barplot(df2[,2], xaxt = "n", ylab= "y-axis", space= 2.0, main ="Space = 2.0")
axis(side = 1, at = bp2[at_tick2] , labels = labels2)
par(mfrow = c(1,1))
Have a look here:
Top layer: bp recalculated every time
Bottom layer: bp space=0 reused
Cutting and pasting the commands in your console may illustrate the effects better than the pic above.
I hope this helps.
You could use the axis function, I used match to obtain the indices of the dates on the axis:
space=1
#Plot bars but without x-axis
barplot(df[,2], names.arg=format(index(df), "%Y"), ann=FALSE, bty="n", tck=-0, xaxt="n",
col=1:1, border=NA, space=space); title(main="Example chart", ylab="y-axis")
# Set x-axis parameters
x_min <- min(index(df))
x_max <- max(index(df))
#Add x-axis
axis(1, at=match(seq(as.Date(x_min), x_max, "years"),index(df))*(1+space),
labels = format(seq(as.Date(x_min), x_max, "years"),"%Y"),lwd=0)
Hope this helps!

Convert an R barplot to plot 2 lines and add data points

I need to draw a 2 line R program plot with data points on one line and a minimum Y value set to -6. The following data is the .csv input file:
> cat verifyRecording.csv
Time,MaxDBK,DBChange
07:30,20,2
07:35,21,1
07:40,22,0
07:45,23,-1
07:50,24,-2
07:55,32,-5
08:00,19,3
Below is an R program that takes the above .csv input and outputs a bar chart with 2 lines. I only need the 2 lines so it needs the barplot converted to a 2 line (plot) chart. Then add the d1$DBChange data points on the DBChange line and set the minimum Y value to -6.
#!/usr/bin/Rscript
d1 <- read.csv(file="verifyRecording.csv",head=T,sep=",")
# Provide an image size which will ensure the x labels display
png(filename="verifyRecording.png", width=1024, bg="white")
# Replace the barplot function with a plot function.
# Fix the Y values be to actually show -6 as the minimum value.
mp <- barplot(d1$MaxDBK, ylim=c(-6,50), main="Sound Recording started: 05/21/2017 7:25 AM", xlab="Time in 24hr", ylab="Sound in Decibals", border='blue')
axis(1,at=mp,labels=d1$Time)
lines(mp,d1$DBChange,type="o",pch=19,lwd=2,col="red")
lines(mp,d1$MaxDBK,type="o",pch=19,lwd=2,col="blue")
# Display the DBChange data values on the d1$DBChange line points.
##### points(d1$Time, d1$DBChange, type="l", col="red")
legend("topright", c("Recommended_DB_Change","Max_DB_Volume"), lty=c(1,1), lwd=c(2.5,2.5), col=c("red","blue"))
dev.off()
You can achieve a plot with only the lines if you start with an empty plot, instead of a barplot. This way, you just add the elements you want in it.
# Empty plot ("type = "n")
# xaxt = "n" in place to allow labelling with axis
# Set y axis limits
plot(1:nrow(d1), d1$MaxDBK, type = "n", ylim = c(-6, 50), xaxt = "n",
main = "Sound Recording started: 05/21/2017 7:25 AM",
xlab = "Time in 24hr",
ylab = "Sound in Decibals")
# Lines added
lines(d1$Time, d1$MaxDBK,type = "o", pch = 19, lwd = 2, col = "blue")
lines(d1$Time, d1$DBChange,type = "o", pch = 19, lwd = 2, col = "red")
# Label added in x axis. Changed at value to show properly
axis(side = 1, labels = d1$Time, at = d1$Time)
# Label each point with the values in d1$DBChange
text(d1$Time, d1$DBChange, labels = d1$DBChange, pos = 3)

Resources