Antoni Parellada
Antoni Parellada

Reputation: 4791

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?

Upvotes: 0

Views: 307

Answers (1)

Ian Campbell
Ian Campbell

Reputation: 24770

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)

enter image description here

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)

enter image description here

Upvotes: 1

Related Questions