Reputation: 401
This is my code. I have used ggplot2. I want to change axis values of y alone as mentioned in the pic below
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
)
Upvotes: 0
Views: 1796
Reputation: 2146
You don't need to alter HSI. You just have to use ylim() ggplot function:
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)
EDIT: Above solution does not work for geom_bar.
The solution needs to define a translate function from the scales library.
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")
Upvotes: 1
Reputation: 1281
You can use scale_y_continuous
to set the breaks and corresponding labels:
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))
This will produce your desired graph (alongwith a warning message: In loop_apply(n, do.ply) : Stacking not well defined when ymin != 0 which can be ignored in this case).
Upvotes: 1