Specify tick marks on y-axis ggplot2
11,209
You have to remove the ylim function and input the scale_y_continuous one
ggplot(data=test, aes(x=Var2, y=value, fill=Var1))+
geom_bar(stat="identity", position=position_dodge(), colour="black")+
scale_fill_manual(values=c("grey80","grey20","blue","lightblue"))+
theme_bw()+
scale_y_continuous(breaks = seq(0, 1, .1), limits = c(0, 1)) +
ggtitle("example")+
#theme(legend.direction="horizontal",
# legend.key.size=unit(18,"points"),
# legend.justification=c(1,1),
# legend.position=c(1,0.25),
# legend.title=element_blank()) +
scale_x_continuous(limits = c(-7.5,7.5), breaks = round(seq(-7,7)),1)+
xlab("position")+ylab("arbitrary value")+
geom_hline(yintercept = 0.5, linetype=3)
Comments
-
biohazard almost 2 years
Here is what my
test
data looks like (this is fake data):Var1 Var2 value 1 A -7 0.7239130 2 C -7 0.8326087 3 G -7 0.7891304 4 U -7 0.8543478 5 A -6 0.7673913 6 C -6 0.8326087 7 G -6 0.7673913 8 U -6 0.8326087 9 A -5 0.8543478 10 C -5 0.7239130 11 G -5 0.8760870 12 U -5 0.7456522 13 A -4 0.8108696 14 C -4 0.7456522 15 G -4 0.8108696 16 U -4 0.8326087 17 A -3 0.8543478 18 C -3 0.7456522 19 G -3 0.8108696 20 U -3 0.7891304 21 A -2 0.7456522 22 C -2 0.8543478 23 G -2 0.8326087 24 U -2 0.7673913 25 A -1 0.8760870 26 C -1 0.8326087 27 G -1 0.5934783 28 U -1 0.8978261 29 A 0 NA 30 C 0 NA 31 G 0 NA 32 U 0 NA 33 A 1 0.6804348 34 C 1 0.9847826 35 G 1 0.8978261 36 U 1 0.6369565 37 A 2 0.9195652 38 C 2 0.7891304 39 G 2 0.7891304 40 U 2 0.7021739 41 A 3 0.7891304 42 C 3 0.7239130 43 G 3 0.8108696 44 U 3 0.8760870 45 A 4 0.7021739 46 C 4 0.8326087 47 G 4 0.7673913 48 U 4 0.8978261 49 A 5 0.6804348 50 C 5 0.8543478 51 G 5 0.8108696 52 U 5 0.8543478 53 A 6 0.7456522 54 C 6 0.7456522 55 G 6 0.7891304 56 U 6 0.9195652 57 A 7 0.8326087 58 C 7 0.8326087 59 G 7 0.7456522 60 U 7 0.7891304
Here is my code for
ggplot2
:ggplot(data=test, aes(x=Var2, y=value, fill=Var1))+ geom_bar(stat="identity", position=position_dodge(), colour="black")+ scale_fill_manual(values=c("grey80","grey20","blue","lightblue"))+ theme_bw()+ ylim(0, 1)+ ggtitle("example")+ theme(legend.direction="horizontal", legend.key.size=unit(18,"points"), legend.justification=c(1,1), legend.position=c(1,0.25), legend.title=element_blank())+ scale_x_continuous(limits = c(-7.5,7.5), breaks = round(seq(-7,7)),1)+ xlab("position")+ylab("arbitrary value")+ geom_hline(yintercept = 0.5, linetype=3)
Here is the output:
I would like to specify the ticks on the y-axis so that it is labeled 0, 0.1, 0.2, 0.3... I tried using
scale_y_continuous
but it erased the entire y-labels. Most tutorials solely focus on the x-axis. How am I supposed to do this?-
joran about 10 yearsI'm not sure exactly what your problem is. Adding
scale_y_continuous(breaks = seq(0,1,by = 0.1))
to your graph works just fine for me (and that's no different that what you did for the x axis). -
biohazard about 10 yearsI did try that at first but I don't know why it didn't work. Then I tried maloneypatr's answer below and it gave me super long values so I used
round()
to counter that and it's working now. I may have made a mistake but I don't think my question is useless for the community, since I couldn't find a question describing y-ticks settings.
-
-
joran about 10 yearsNot using both
ylim
andscale_y_continuous
may be cleaner, but it is not necessary: addingscale_y_continuous
works just fine even if you leaveylim
in (although you do get a warning, of course). -
biohazard about 10 yearsIs there a reason why you commented out
theme()+
? -
biohazard about 10 yearsWhen I implement your solution with
scale_y_continuous(breaks = seq(-0.3, 0.3, .1), limits = c(-0.35, 0.35)) +
, I get super long values on the y-axis such as3.000000e-1, 2.000000e-1, 1.000000e-1, 5.551115e-17...
-
biohazard about 10 yearsOh, nevermind, it works perfectly with
round(seq(-0.3,0.3,.1),2)
! Maybe you can add it to your solution :D -
Elisabeth about 6 yearsIs there a way to add a custom y tick mark at one specific point (to call out a special y value on the graph)? Thank you