Chris2015
Chris2015

Reputation: 1050

How to setup a table with TRUE/FALSE or 1/0 values?

I am new to R and my goal is to set up a line graph based on the values in a table. Here is the format of my table:

             0    1
  2017-02    1    2
  2017-03    0    2
  2017-04    0    2
  2017-10    0    2

The first column is the date, second column represents FALSE values, and the third column represents TRUE values. After creating this table, I tried plotting the values in the table as-is, but the graph showed both columns labelled "0" and "1", when really I only want to plot for the column labelled "1". In order to get around this, I updated the table to exclude column labelled "0". Here is what I have:

tbl<-with(COMP43,table(COMP43$Date,COMP43$Retention))
tbl<-tbl[,2]
plot(tbl,type="l",main="Approved Retentions",lwd=3,xlab="Date")

With this code, I have successfully plotted the graph, except now my x-axis values do not show the dates but rather 0, 5, 10, 15. Is there an easier way to plot this? Or a way to show the dates on the x-axis?

Ultimately, I'd like a more advanced graph with interactivity using htmlwidgets. I don't know if that impacts any plotting solution I start out with.

Thanks much!

Here is what I show for Dput(tbl):

dput(tbl)
c(`2017-02` = 2L, `2017-03` = 2L, `2017-04` = 2L, `2017-10` = 2L, 
`2017-11` = 1L, `2018-01` = 3L, `2018-02` = 0L, `2018-03` = 3L, 
`2018-04` = 9L, `2018-05` = 2L, `2018-06` = 166L, `2018-07` = 18L, 
`2018-08` = 8L, `2018-09` = 7L, `2018-10` = 51L, `2018-11` = 11L, 
`2018-12` = 8L, `2019-01` = 7L, `2019-02` = 38L, `2019-03` = 9L, 
`2019-04` = 34L, `2019-05` = 33L, `2019-06` = 39L, `2019-07` = 41L, 
`2019-08` = 40L, `2019-09` = 37L, `2019-10` = 154L, `2019-11` = 38L, 
`2019-12` = 30L, `2020-01` = 26L, `2020-02` = 59L, `2020-03` = 23L, 
`2020-04` = 14L)

Upvotes: 0

Views: 440

Answers (1)

Dominic van Essen
Dominic van Essen

Reputation: 872

First: you don't need to delete unwanted columns from your data.frame or matrix in order to selectively plot a particular column: just specify to plot which column you it is that want to plot:

plot(my_matrix[,2])

Second: plotting the tick labels the way that one wants them isn't always very simple in R (unfortunately). Often, one needs to do it in 2 steps - first plot the graph without the axis, and then plot the axis. This becomes even more awkward if you want to label the ticks with long text labels, as you do here. But the good news is that one can wrap it all into a clean function.

Here is an example:

First, plot the graph with no x-axis:

plot(tbl,type="o",xaxt="n",xlab="",ylab="TRUE values")
# I chose type="o" overlaid lines & points
# xaxt="n" specifies to not plot the x-axis

Then, we make a function to add textual x-axis labels:

x_axis_labels=function(labels,every_nth=1,...) {
    axis(side=1,at=seq_along(labels),labels=F)
    text(x=(seq_along(labels))[seq_len(every_nth)==1],
        y=par("usr")[3]-0.075*(par("usr")[4]-par("usr")[3]),
        labels=labels[seq_len(every_nth)==1],xpd=TRUE,...)
}
# axis() draws the axis with ticks at positions specified by at.  Again, we don't plot the labels yet.
# text() plots the labels at positions given by x and y.
# We estimate the y-positions from the values of the y-axis (using par("usr")),
# and specify xpd=TRUE to indicate that we don't want to crop plotting to within the plot area
# Note that we select the [seq_len(every_nth)==1] elements of both the x positions and the labels, 
# so we can easily skip labels if there would be too many to cram in otherwise.  
# Finally, we leave a ... in the function so we can pass additional arguments to text()

Finally, we call the new function to plot the axis tick labels:

x_axis_labels(labels=names(tbl),every_nth=4,adj=1,srt=45)

Here we take advantage of the ... in the function to pass two more parameters: adj=1 specifies to right-justify the text labels, and srt=45 indicates to rotate them by 45 degrees.

Upvotes: 0

Related Questions