Grace_G
Grace_G

Reputation: 53

Barplot by R, the scale (mark, correspond to labels) does not correspond to each bar?

But they are same length vector, I have tried many part of code, just can't let the "city name" correspond each bar? The problem of pdf width? Or I shouldn't axis(1, c(1:47)? Tried but don't solve...Reminder also will be grateful.

pdf("barplot1.pdf", width = 8.6, height = 8)  

matrix_tem <- t(matrix(each_bar_number))    #each_bar_number is a vector, length(each_bar_number) is 47
barplot(matrix_tem, ylim = c(0, 150), xaxt="n", border=NA, space = NULL, beside=F)
axis(1, c(1:47), labels=as.character(cityname_vector), las = 2,cex.axis = 0.6) #length(cityname_vector) is 47  

dev.off()

Upvotes: 0

Views: 34

Answers (1)

astaines
astaines

Reputation: 922

barplot() expects the names to be in the names.arg parameter inside the call to barplot. See here for an example. Not sure why the axis labelling fails.

## Default S3 method:

 barplot(height, width = 1, space = NULL,
    names.arg = NULL, legend.text = NULL, beside = FALSE,
    horiz = FALSE, density = NULL, angle = 45,
    col = NULL, border = par("fg"),
    main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
    xlim = NULL, ylim = NULL, xpd = TRUE, log = "",
    axes = TRUE, axisnames = TRUE,
    cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
    inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,
    add = FALSE, args.legend = NULL, ...)

Arguments

 names.arg
   a vector of names to be plotted below each bar or group of bars.
   If this argument is omitted, then the names are taken from the
   names attribute of height if this is a vector, or the column 
   names if it is a matrix.

Upvotes: 1

Related Questions