How to Specify the Actual X Axis Values to Plot as X Axis Ticks in R

How to specify the actual x axis values to plot as x axis ticks in R

You'll find the answer to your question in the help page for ?axis.

Here is one of the help page examples, modified with your data:

Option 1: use xaxp to define the axis labels

plot(x,y, xaxt="n")
axis(1, xaxp=c(10, 200, 19), las=2)

Option 2: Use at and seq() to define the labels:

plot(x,y, xaxt="n")
axis(1, at = seq(10, 200, by = 10), las=2)

Both these options yield the same graphic:

Sample Image


PS. Since you have a large number of labels, you'll have to use additional arguments to get the text to fit in the plot. I use las to rotate the labels.

Replace X-axis with own values

Not sure if it's what you mean, but you can do this:

plot(1:10, xaxt = "n", xlab='Some Letters')
axis(1, at=1:10, labels=letters[1:10])

which then gives you the graph:

Sample Image

ggplot x-axis labels with all x-axis values

Is this what you're looking for?

ID <- 1:50
A <- runif(50,1,100)

df <- data.frame(ID,A)

ggplot(df, aes(x = ID, y = A)) +
geom_point() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
scale_x_continuous("ID", labels = as.character(ID), breaks = ID)

This will produce this image:

Sample Image

So you'll get a label for every ID-value. If you'd like to remove the gridlines (There are too much for my taste) you can remove them by adding theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

EDIT: The easier way would be to just use ID as a factor for the plot. like this:

ggplot(df, aes(x = factor(ID), y = A)) + 
geom_point() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
xlab("ID")

Sample Image

The advantage of this method is that you don't get empty spaces from missing IDs

EDIT2: Concerning your Problem with overlapping labels: I'm guessing it comes from a large number of IDs to be plotted. There are several ways we can deal with this. So lets say your plot looks like this:

Sample Image

One idea would be to hide every 3rd label from the x-axis by modifying the break argument of the axis:

ggplot(df, aes(x = factor(ID), y = A)) + 
geom_point() +
scale_x_discrete(breaks = ID[c(T,F,F)]) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
xlab("ID")

which leads to this:

Sample Image

If hiding labels is not an option, you could split your plot into subplots.

df$group <- as.numeric(cut(df$ID, 4))

ggplot(df, aes(x = factor(ID), y = A)) +
geom_point() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
xlab("ID") +
facet_wrap(~group, ncol = 1, scales = "free_x")

which leads to this:

Sample Image

how to specify x-axis value for date in R

Please see below, i just replicated your example with dummy data

> df
# A tibble: 55 x 2
datetime Freq
<dttm> <int>
1 2018-11-01 12:41:57 215
2 2018-11-01 12:41:58 163
3 2018-11-01 12:47:06 225
4 2018-11-01 12:51:00 69
5 2018-11-01 12:57:37 203
6 2018-11-01 12:57:38 248
7 2018-11-01 12:57:38 58
8 2018-11-01 13:29:15 179
9 2018-11-01 13:37:45 233
10 2018-11-01 14:24:43 150
# ... with 45 more rows

And the code to kind of plot you are expecting with x-axis as timestamp and you can give whichever format you want

plot(df$datetime,df$Freq,xaxt="n")
axis.POSIXct(1, at=df$datetime, labels=format(df$datetime, "%m/%d/%Y %H:%M:%S"))

Output image with x-axis as timestamp

Changing X-Axis Values in r

Here's a solution with minimal changes to your example. Basically, I create a data.frame with the observation number. Since the plot puts all observations in decreasing order, I also do that to the newly created data.frame. This is what I use in the axis call.

result <-c(0.6256767,0.6833695,0.7671350,0.5205373,0.4932262,0.5852338,0.5088692,0.3379572,
0.3420370,0.3029084,0.4677624,0.4822537,0.3047485,0.3852572,0.3186014,0.2009436,
0.1882227,0.2090007,0.2654110,0.3334744)

result_df <-data.frame(my_order=1:length(result),result=result) #add column with initial observation number
result_df <-result_df[order(result_df$result, decreasing = TRUE),] #decreasing order

plot(cbind(result, result),xlim=c(max(result),min(result)),
ylim=c(min(result),max(result)), xaxt="n")
axis(1, at=result_df$result,labels=result_df$my_order,cex.axis=0.6)

Sample Image

How to change the (number of) x-axis ticks in a plot.zoo plot? (axis() fails)

Here is a version based on standard plot():

plot(as.numeric(ts), type = "l", xaxt = "n")
ticks <- axTicksByTime(index(ts), format.labels = "%Y-%m-%d")
labels <- names(ticks)
axis(1, at = ticks, labels = labels)

And a somewhat nicer one:

plot(as.numeric(ts), type = "l", xaxt = "n", xlab = "", ylab = "ts")
ticks <- axTicksByTime(index(ts), format.labels = "%Y-%m")
labels <- names(ticks)
axis(1, at = ticks, labels = labels, las = 2)


Related Topics



Leave a reply



Submit