Using the Coord_trans function allows you to remove data from the chart.

I am trying to build my data using ggplot2, but the data from my x axis should be converted to a log. I tried to use the coord_trans function because I want to keep the original labels and just convert my coordinates, but when I do this, all my points will disappear from my graph. I have no idea why this is happening? Maybe because there is no zero in my data? And how can I fix this?

Here is my input for creating ggplot:

    library(afex)
    library(car)
    library(MASS)
    library(rockchalk)
    library(multcomp)
    library(lsmeans)
    library(gplots)
    library(ggplot2)
    library(tidyr)
    library(effects)
    library(scales)
    library(ggthemes)
    library(gtools)

    theme_set(theme_few(base_size=14)) 

    p=ggplot(data, aes(x=day, y=(propinfected), linetype=treatment, group=treatment)

   p + geom_smooth(aes(fill=treatment),colour="black", method="glm",
   family="quasibinomial")+ 
   coord_trans(xtrans = "log10")+
   geom_point(aes(fill=treatment),size=5,pch=21)+
   labs(x="Day", y="Above detection treshold", size=10)+
   scale_y_continuous(labels= percent,breaks=seq(0,1,by=0.2),limits=c(0,1))+ 
   scale_x_continuous(breaks=seq(0,16,by=4),limits=c(0,16))+
   scale_fill_manual(values=c("black","grey"))+
   theme(legend.justification=c(1,0), legend.position=c(1,0),
         legend.title=element_blank(),axis.text=element_text(size=20),
         axis.title=element_text(size=20),legend.text=element_text(size=15))

This is the model I'm working with:

fitlog=glm(propinfected~log10(day+1)*treatment,family=quasibinomial(link=logit), data=data)

This is the data I used:

dput(data)

structure(list(treatment = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L), .Label = c("CTRL", "DWV"), class = "factor"), 
day = c(0L, 0L, 4L, 4L, 8L, 8L, 12L, 12L, 16L, 16L), infected = c(0L, 
20L, 11L, 20L, 15L, 18L, 16L, 19L, 19L, 19L), not_infected = c(20L, 
0L, 9L, 0L, 5L, 2L, 4L, 1L, 1L, 1L), propinfected = c(0, 
1, 0.55, 1, 0.75, 0.9, 0.8, 0.95, 0.95, 0.95)), .Names = c("treatment", 
"day", "infected", "not_infected", "propinfected"), row.names = c(NA, 
-10L), class = "data.frame")

When I draw a graph using the allEffects function, I get the correct plot with the correct lines and confidence ranges. But I want to do this in ggplot, because the graph of allEffects is not so good.

plot(allEffects(fitlog),ylab="Above detection treshold",type="response")

Thanks for the help!

enter image description here

+4
2

formula in geom_smooth , glm, coord_trans. y ~ log10(x + 1).

ggplot(data, aes(x=day, y=propinfected, linetype=treatment, group=treatment)) + 
    geom_smooth(aes(fill=treatment), formula = y ~ log10(x + 1), 
                colour="black", method="glm",
                method.args = list(family="quasibinomial")) +
    geom_point(aes(fill=treatment),size=5,pch=21) +
    labs(x="Day", y="Above detection treshold", size=10) +
    scale_y_continuous(labels= percent,breaks=seq(0,1,by=0.2),limits=c(0,1)) +
    scale_x_continuous(breaks=seq(0,16,by=4),limits=c(0,16)) +
    scale_fill_manual(values=c("black","grey")) +
    theme(legend.justification=c(1,0), legend.position=c(1,0),
          legend.title=element_blank(), axis.text=element_text(size=20),
          axis.title=element_text(size=20), legend.text=element_text(size=15))

, , scale_x, coord_trans. , coord_trans :

, .

, x , . , - , . , , , .

scale_x_log10 coord_trans:

ggplot(data, aes(x=day + 1, y=(propinfected), linetype=treatment, group=treatment)) + 
    geom_smooth(aes(fill=treatment), 
                colour="black", method="glm",
                method.args = list(family="quasibinomial")) +
    geom_point(aes(fill=treatment),size=5,pch=21) +
    labs(x="Day", y="Above detection treshold", size=10) +
    scale_y_continuous(labels= percent,breaks=seq(0,1,by=0.2),limits=c(0,1)) +
    scale_x_log10(labels = seq(0,16,by = 4), breaks=seq(1,17,by=4), limits=c(1,17)) +
    coord_trans(x = exp_trans(base = 10)) 
+1

, , 0, 0 - undefined. , , .

# Here we add 1 to the day:
    p <- ggplot(data, aes(x=day+1, y=(propinfected), linetype=treatment, group=treatment))

      # and here we use breaks 1 to 17, but labels 0 to 16. 
      p + geom_smooth(aes(fill=treatment),colour="black", method="glm",
                     family="quasibinomial")+ 
       coord_trans(xtrans = "log10")+
       geom_point(aes(fill=treatment),size=5,pch=21)+
       labs(x="Day", y="Above detection treshold", size=10)+
       scale_y_continuous(breaks=seq(0,1,by=0.2),limits=c(0,1))+ 
       scale_x_continuous(labels=seq(0,16,by=4),breaks=seq(1,17,by=4),limits=c(1,17))+
       scale_fill_manual(values=c("black","grey"))+
       theme(legend.justification=c(1,0), legend.position=c(1,0),
             legend.title=element_blank(),axis.text=element_text(size=20),
             axis.title=element_text(size=20),legend.text=element_text(size=15))

( , ).

0

Source: https://habr.com/ru/post/1622553/


All Articles