这是我的代码。我用过ggplot2。我想单独更改y的轴值,如下图所示
library(ggplot2)
rm(list=ls())
bar=read.csv("Age.csv")
attach(bar)
Category=sub('\\s+$', '', Category)
HSI = HSI-100
df = data.frame(HSI=HSI,Category)
ggplot(df, aes(x=Category,y=HSI, fill=Category)) +
geom_bar(stat = "identity", aes(width=0.3)) + # adjust width to change thickness
geom_text(aes(label=HSI+100, y=HSI+2*sign(HSI)),# adjust 1.1 - to change how far away from the final point the label is
size=5 # adjust the size of label text
)
答案 0 :(得分:1)
您可以使用scale_y_continuous
设置中断和相应的标签:
ggplot(df, aes(x=Category,y=HSI, fill=Category)) +
geom_bar(stat = "identity", aes(width=0.3)) + # adjust width to change thickness
geom_text(aes(label=HSI+100, y=HSI+2*sign(HSI)), size = 5) +
scale_y_continuous(breaks = seq(-20, 30, 10), labels = 100 + seq(-20, 30, 10))
这将生成您想要的图形(以及一条警告消息: In loop_apply(n,do.ply):当ymin!= 0 时,堆栈没有很好地定义,在这种情况下可以忽略)。
答案 1 :(得分:1)
您无需更改HSI。你只需要使用ylim()ggplot函数:
ggplot(df, aes(x=Category,y=HSI, fill=Category)) +
geom_bar(stat = "identity", aes(width=0.3)) +
geom_text(aes(label=HSI, y=HSI+2*sign(HSI)),
size=5)+
ylim(100,130)
编辑:以上解决方案不适用于geom_bar。
解决方案需要从比例库中定义一个翻译函数。
library(scales)
translate100_trans <- function() {
trans <- function(x) x - 100
inv <- function(x) x + 100
trans_new("translate100_trans", trans, inv)
}
ggplot(df, aes(x=Category,y=HSI, fill=Category)) +
geom_bar(stat = "identity", aes(width=0.3)) +
geom_text(aes(label=HSI, y=HSI+2*sign(HSI)),
size=5)+scale_y_continuous(trans="translate100")