简体   繁体   中英

R plotting : row names as labels

I have a single variable that I want to plot, lets say the temperature in a place. Instead of the 'index =1,2,3.." in the horizontal axis, I want the name of place that I have in another column (corresponding to temperature at that place) instead of the 1,2,3. Is there a way to do this ?

something like this :

place1 32

place2 33

place3 43

place4 37

Basically I want to be able to use a column as labels for a plot.

Assuming your data is:

temp <- data.frame(temperature = c(32,33,43,37), 
                   place = paste("Place", 1:4))

That is:

  temperature   place
1          32 Place 1
2          33 Place 2
3          43 Place 3
4          37 Place 4

You can use:

# Create a scatterplot, with an hidden x axis
plot(temp$temperature, pch=20, ylim=c(0, 50), 
     xaxt="n", xlab="Place", ylab="Temperature")
# Plot the axis separately
axis(1, at=1:4, labels=temp$place)

Or, if you want a barplot

barplot(temp$temperature, names.arg=rownames(temp$place))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM