具有不同颜色负值的条形图
Barplot with negative values in different color
我想为包含正值和负值的数据框绘制条形图,负值应为红色。
我想避免使用 ggplot 并使用基本的 'barplot'
有什么建议吗?
这是我的代码
df <- data.frame(MM=(1:12), value=rnorm(12))
df
toplot <- df
options(repr.plot.width=6, repr.plot.height=4)
label <- seq(1:12)
a <-barplot(df$value,
names=label,
col='deepskyblue3',
xaxt = "n", yaxt = "n",
)
axis(1, cex.axis=0.55, las=1, at=a, labels=label)
axis(2, las='2', cex.axis=0.6)
abline(h = 0, col = "black")
将 ifelse
条件添加到 col
a <- barplot(df$value,
names=label,
col= ifelse(df$value < 0,"red","blue"),
xaxt = "n", yaxt = "n"
)
参考Change colours of particular bars in a bar chart
我想为包含正值和负值的数据框绘制条形图,负值应为红色。
我想避免使用 ggplot 并使用基本的 'barplot'
有什么建议吗?
这是我的代码
df <- data.frame(MM=(1:12), value=rnorm(12))
df
toplot <- df
options(repr.plot.width=6, repr.plot.height=4)
label <- seq(1:12)
a <-barplot(df$value,
names=label,
col='deepskyblue3',
xaxt = "n", yaxt = "n",
)
axis(1, cex.axis=0.55, las=1, at=a, labels=label)
axis(2, las='2', cex.axis=0.6)
abline(h = 0, col = "black")
将 ifelse
条件添加到 col
a <- barplot(df$value,
names=label,
col= ifelse(df$value < 0,"red","blue"),
xaxt = "n", yaxt = "n"
)
参考Change colours of particular bars in a bar chart