Error generating graph with package ggplot2 in R

Asked

Viewed 250 times

2

Hello, I am looking for an aid to improve a script in R. I develop it with the aid of the ggplot2 package. I intend to show a graph with media and standard error and the data set points. I used the layers "geom_errorbar()" and "geom_dotplot()" .

I have found the error : "Error in FUN(X[[i]], ...) : Object 'PV. y' not found".

I think the error is in the layer "geom_errorbar()". Can anyone tell me what is wrong? Follow the example in the script:

gmed <-read.csv("https://drive.google.com/open?id=1RSpq7Tpxzt_eHEwwWzh2vPJJ1ddU1I0N",header=T, sep=',')
##gráfico
require(Rmisc)
require(ggplot2)
require(EnvStats)
require(sciplot)
require(dplyr)
#
gpv <- summarySE(gmed, measurevar="PV", groupvars=c("INT"))
summarygpv <- ggplot( gpv, aes(x="INT", y= "PV", fill= "INT"))+
  scale_x_discrete(limits=c("S", "P", "G", "F"))+ #alterar ordem ítens 
legenda
  geom_dotplot(binwidth= 0.5,
               binaxis="y",
               stackdir = "center") +
  geom_errorbar(aes (ymin=PV.y-se,
                     ymax=PV.y+se),
                width = 0.25,
                size=0.25)+
  xlab("Tratamentos") +
  ylab(" Posição Vertical (cm)") +
  geom_text(aes(label = paste("N", "==",N,sep = "")),
            parse = TRUE,
            y=-0.15)+
  geom_point(aes(y=PV.y),
             size=1,show.legend = F) +
  theme_bw () +
  scale_fill_manual(values=c("grey75","grey25"))
summarygpv
  • So that the error appears the variable "PV. y" was not found, check if this is the same name in gpv.

1 answer

1


The main problem was to call the variables to be plotted as strings, placing them in quotes. See that in my code below I call them directly.

ggplot(gpv, aes(x=INT, y=PV, fill=INT)) +
  scale_x_discrete(limits=c("S", "P", "G", "F")) + 
  geom_dotplot(binwidth= 0.5, binaxis="y", stackdir = "center") +
  geom_errorbar(aes(ymin=PV-se, ymax=PV+se), width=0.25, size=0.25) +
  labs(x="Tratamentos", y="Posição Vertical (cm)") +
  geom_text(aes(label=paste("N", "==",N,sep = "")), parse = TRUE, y=c(17.5, 15, 15, 22.5)) +
  geom_point(aes(y=PV), size=1, show.legend = F) +
  theme_bw () +
  scale_fill_grey(start=0.25, end=0.75)

inserir a descrição da imagem aqui

In addition to this amendment, I have changed the position of the texts N=?. I think it looks better this way, because the graphic window has a dot very far from the others. You could put below (or above) each of the Dots, but then you have to increase the limits of the graph so that there is no lack of space.

Parallel to that, I don’t know the geom_point() is necessary as it leaves a black dot in the middle of the larger Dots. I kept on my result but I would take off the final graph.

Finally, to put scale_fill_grey, with initial and final gray values, it is more practical than setting a different gray value for each level of the Treatment variable.

  • grateful for the input and tips. However, I would like to have the observations represented on the graph by dots next to the midpoint and error bars.

  • For this implementation would have a more appropriate layer eg, geom_dotplot() or geom_point() ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.