具有所有打印 x 值的 R 软件直方图
R software histogram with all printed x values
我有以下数据
meterpiuimportanti[row_sub,]
Meter Numero Nodi
[1,] 608 107
[2,] 51 89
[3,] 197 81
[4,] 52 81
[5,] 192 21
[6,] 110 14
[7,] 171 13
[8,] 114 12
[9,] 252 11
[10,] 121 10
[11,] 94 10
[12,] 295 9
[13,] 341 9
[14,] 113 7
[15,] 118 5
[16,] 196 4
[17,] 91 3
[18,] 92 3
[19,] 96 3
[20,] 112 3
[21,] 345 3
[22,] 378 3
[23,] 386 3
[24,] 90 2
[25,] 105 2
[26,] 204 2
[27,] 374 2
[28,] 104 1
[29,] 287 1
[30,] 328 1
[31,] 414 1
我想要一个整页 (1024x768) 直方图,其中 x 轴是第一列,Y 轴是第二列。
问题是:
1) 不知道怎么放大页面
2) 我希望所有 x 值都必须打印在 x 轴上,并且在直方图的每个框的顶部我想打印 y
的值
感谢您的帮助
查看下面的代码。它使用 grDevices 包。我不太记得了,但我认为它是基本安装附带的。
df <-read.csv("/Data/test1.csv") #read
png(filename="output.png", width=1024, height=768) #open graphics
df <- df[order(df$x),] #order data source
mp <- barplot(df$y,axes=F) #plot w/o labels
#add value labels
text(cex=1.5, x=mp, y=df$y+par("cxy")[2]/2+1, round(df$y,2), xpd=TRUE)
axis(1,at=mp,labels=df$x, las=2) #add x labels, make'm vertical
axis(2,seq(0,max(df$y),round(max(df$y)/20))) #add y labels
dev.off()
您可以使用 ggplot2
包:
代码
library(ggplot2)
png('~/x.png',width=1024,height=768)
ggplot(d) +
aes(x=factor(V1,levels=V1),y=V2) +
geom_bar(position='dodge',stat='identity') +
xlab('V1') +
geom_text(aes(label=V2), position=position_dodge(width=0.9), vjust=-0.25)
dev.off()
结果
数据集
d <- structure(list(V1 = c(608L, 51L, 197L, 52L, 192L, 110L, 171L,
114L, 252L, 121L, 94L, 295L, 341L, 113L, 118L, 196L, 91L, 92L,
96L, 112L, 345L, 378L, 386L, 90L, 105L, 204L, 374L, 104L, 287L,
328L, 414L), V2 = c(107L, 89L, 81L, 81L, 21L, 14L, 13L, 12L,
11L, 10L, 10L, 9L, 9L, 7L, 5L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA,
-31L))
我有以下数据
meterpiuimportanti[row_sub,]
Meter Numero Nodi
[1,] 608 107
[2,] 51 89
[3,] 197 81
[4,] 52 81
[5,] 192 21
[6,] 110 14
[7,] 171 13
[8,] 114 12
[9,] 252 11
[10,] 121 10
[11,] 94 10
[12,] 295 9
[13,] 341 9
[14,] 113 7
[15,] 118 5
[16,] 196 4
[17,] 91 3
[18,] 92 3
[19,] 96 3
[20,] 112 3
[21,] 345 3
[22,] 378 3
[23,] 386 3
[24,] 90 2
[25,] 105 2
[26,] 204 2
[27,] 374 2
[28,] 104 1
[29,] 287 1
[30,] 328 1
[31,] 414 1
我想要一个整页 (1024x768) 直方图,其中 x 轴是第一列,Y 轴是第二列。
问题是:
1) 不知道怎么放大页面
2) 我希望所有 x 值都必须打印在 x 轴上,并且在直方图的每个框的顶部我想打印 y
感谢您的帮助
查看下面的代码。它使用 grDevices 包。我不太记得了,但我认为它是基本安装附带的。
df <-read.csv("/Data/test1.csv") #read
png(filename="output.png", width=1024, height=768) #open graphics
df <- df[order(df$x),] #order data source
mp <- barplot(df$y,axes=F) #plot w/o labels
#add value labels
text(cex=1.5, x=mp, y=df$y+par("cxy")[2]/2+1, round(df$y,2), xpd=TRUE)
axis(1,at=mp,labels=df$x, las=2) #add x labels, make'm vertical
axis(2,seq(0,max(df$y),round(max(df$y)/20))) #add y labels
dev.off()
您可以使用 ggplot2
包:
代码
library(ggplot2)
png('~/x.png',width=1024,height=768)
ggplot(d) +
aes(x=factor(V1,levels=V1),y=V2) +
geom_bar(position='dodge',stat='identity') +
xlab('V1') +
geom_text(aes(label=V2), position=position_dodge(width=0.9), vjust=-0.25)
dev.off()
结果
数据集
d <- structure(list(V1 = c(608L, 51L, 197L, 52L, 192L, 110L, 171L,
114L, 252L, 121L, 94L, 295L, 341L, 113L, 118L, 196L, 91L, 92L,
96L, 112L, 345L, 378L, 386L, 90L, 105L, 204L, 374L, 104L, 287L,
328L, 414L), V2 = c(107L, 89L, 81L, 81L, 21L, 14L, 13L, 12L,
11L, 10L, 10L, 9L, 9L, 7L, 5L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA,
-31L))