Janet Simons
Janet Simons

Reputation: 21

geom_text esthetics must be either length 1 or the same as the data

I am trying to add static labels for scale to a ggplot2 object. I can get the output I want with the following code but it seems like there should be a more efficient way of adding a series of text labels.

ward_stat<-structure(list(Month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 
9, 10, 11, 12), Interval = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("1", 
"2", "3"), class = "factor"), Minutes = structure(c(22, 22, 22, 
22, 21, 23, 21, 21, 21, 22, 23, 22, 13, 13, 14, 13, 10, 11, 10, 
10, 11, 11, 12, 12, 14.4, 13.7, 13.6, 14, 14.7, 16, 14.8, 12.9, 
12.5, 11.3, 12.8, 14), class = "numeric")), .Names = c("Month", 
"Interval", "Minutes"), row.names = c(NA, -36L), class = c("tbl_df", 
"tbl", "data.frame"))



ggplot(data=ward_stat,aes(x=Month,y=Minutes,fill=Interval))+
    geom_bar(stat="identity",width=1,colour="black",size=0.1)+
    coord_polar()+
    geom_hline(yintercept=seq(10,60,10), alpha=0.5, colour="white")+
    geom_text(x=2.5, y=10, label=10) +geom_text(x=2.5, y=20, label=20)+  
    geom_text(x=2.5, y=30, label=30) +geom_text(x=2.5, y=40, label=40)+
    geom_text(x=2.5, y=50, label=50) +geom_text(x=2.5, y=60, label=60)

Desired Output

trying geom_text(x=2.5, y=seq(10,60,10), label=seq(10,60,10)) gives the error Error: Aesthetics must be either length 1 or the same as the data (36): x, y, label

Upvotes: 0

Views: 2101

Answers (1)

AnnVyrgyl
AnnVyrgyl

Reputation: 56

You could save your labels as an object, like

x<-2.5
y<-c(10,20,30,40,50,60)
labels<-c(10,20,30,40,50,60)

plot.labels<-data.frame(x,y,labels)

ggplot(data=ward_stat,aes(x=Month,y=Minutes,fill=Interval))+
    geom_bar(stat="identity",width=1,colour="black",size=0.1)+
    coord_polar()+
    geom_hline(yintercept=seq(10,60,10), alpha=0.5, colour="white")+
    geom_text(data=plot.labels, aes(x=x, y=y, label=label))

It would at least make the plot code slightly cleaner.

Upvotes: 1

Related Questions