0

I have a simple dataset: 11 observations, 1 variable.

I want to plot them adding my own axis names, but when I want to change the position of them, R keeps plotting them in the exact same spot.

Here is my script:

plot(data[,5], xlab = "", xaxt='n')
axis(1, at = 1:11, labels = F)
text(1:11, par("usr")[3] - 0.1, srt = 90, adj = 1, labels = names, xpd = TRUE)

I am changing the -0.1, to any number but R keeps placing the labels in the exact same spot. I tried with short names like "a" but the result is the same.

Thanks in advance

My data:

10308.9
10201.6
12685.3
3957.93
7677.1
9671.7
11849.4
10755.7
11283.4
11583.8
12066.9

names <- rep("name",11)
7
  • why not use axis to make your axis labels instead of text? Commented Apr 9, 2014 at 11:41
  • also, ?mtext is for plot margins Commented Apr 9, 2014 at 11:41
  • Is there a way to change the positions of the labels using axis? I explored the ?axis page but couldn't find it. If the problem can be solved using labels that would great! Commented Apr 9, 2014 at 11:43
  • the at arg is where the labels will go Commented Apr 9, 2014 at 11:44
  • I am trying with mtext and axis but I can't do it. Could you show me which parameters to use so I can have the mtext the same way I wanted it using text? Commented Apr 9, 2014 at 11:53

1 Answer 1

1

My ggplot solution:

# creating the sample dataframe
data <- read.table(text="10308.9
10201.6
12685.3
3957.93
7677.1
9671.7
11849.4
10755.7
11283.4
11583.8
12066.9", header=FALSE)

# adding a names column
data$names <- as.factor(paste0("name",sprintf("%02.0f", seq(1,11,1))))

#creating the plot
require(ggplot2)
ggplot(data, aes(x=names, y=V1)) + 
  geom_bar(fill = "white", color = "black")

which gives: enter image description here

When you want to change the order of the bars, you can do that with transform:

# transforming the data (I placed "name04" as the first one)
data2 <- transform(data,
                   newnames=factor(names,
                                   levels=c("name04","name01","name02","name03","name04","name05","name06","name07","name08","name09","name10","name11"),
                                   ordered =TRUE))

#creating the plot
ggplot(data2, aes(x=newnames, y=V1)) + 
  geom_bar(stat="identity", fill="white", color="black")

which gives: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.