GFauxPas GFauxPas - 2 months ago 6
R Question

ggplot2 code stopped working in 3.3.x

The following code works in

R 3.0.2
but when I try to run the code in
3.3.1
I get the error
Error: Aesthetics must be either length 1 or the same as the data (14): x, y
. I've tried reading the documentation on
aes
but I wasn't able to fix it. How do I update my code to work in
3.3.1
?

library(ggplot2)
V<-c(4.1,4.9,5.78,6.83,7.79,8.93,9.82)


I<-c(4,4.92,5.8,6.86,7.82,8.93,9.86)*(1E-3)


lm0<-lm(I~V+0)

lsrl0<-function(V) lm0$coef[1]*V

ggplot(data.frame(x=c(3E-3, 10E-3)),y=c(3.5,10),aes(x)) +
stat_function(fun=lsrl0,linetype='twodash',color='blue')+
geom_point(data=data.frame(c(V,I)), aes(x = V, y = I),
shape=21,size=3.5,color='dark blue',fill='light blue')+
xlim(3,10)+theme_bw()

Answer

This works, is this the desired output (instead of using c(V,I) for geom_point just use (V,I) for the data frame, was there a typo)?

ggplot(data.frame(x=c(3E-3, 10E-3)),y=c(3.5,10),aes(x)) + 
      stat_function(fun=lsrl0,linetype='twodash',color='blue')+
      geom_point(data=data.frame(V,I), aes(x = V, y = I),
                 shape=21,size=3.5,color='dark blue',fill='light blue')+
      xlim(3,10)+theme_bw()

enter image description here

Comments