2

I am using ggplot2 to create a Forest Plot on a mac running elcapitan. There are faint vertical lines in the background-- these are not grid lines. This is the "theme" setting:

theme_set(theme_bw())
theme_update(
    axis.line = element_line(colour = "black"),
    axis.line.y=element_line(colour="white"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    plot.margin = unit(c(0,0,0,0), "lines")
)

Any ideas on how to get rid of these grey lines? Am a newbie at using this board and R and ggplot2-- Happy to load sample data and my code-- but not sure if useful here.

Screenshot of plot:

enter image description here

UPDATE in response to Diego's question for the code:

This code was based on code from Matt's Stats and stuff -- https://mcfromnz.wordpress.com/2012/11/06/forest-plots-in-r-ggplot-with-side-table/ -- but I can't find the link to his original code.

theme_set(theme_bw())
theme_update(
    axis.line = element_line(colour = "black"),
    axis.line.y=element_line(colour="white"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    plot.margin = unit(c(0,0,0,0), "lines")
)
pD<- ggplot(datD,aes(cen,rev(author2))) + 
     geom_point(aes(fill = summary), size=1.7,shape=23,colour="black") +
     geom_errorbarh(
         aes(xmax = datD$high, xmin = datD$low, colour=summary),
         height = 0.3, size=0.25) +
     scale_x_continuous(
         breaks = seq(0,1,.1), labels = seq(0,100,10)) +
     labs(x="Risk, %", y="") +
     theme(axis.text.x=element_text(size=2))+geom_text(aes(0, author2), label=(paste(rev(datD$author2),rev(datD$risk))), hjust=0, size=ifelse(rev(datD$author2)=="Heterogeneity (95% CI)",2,0), fontface="bold")

p2D<-pD+ 
    theme(legend.position = "none", 
         axis.text.x = element_text(size=6),
         axis.title = element_text(size=6,face="italic")) + 
    guides(size = FALSE) +
    geom_hline(aes(yintercept=c((length(datD$authorREF2)-0.5))))
    
#Left hand side will be author_yr, Events, Participants
  #V0 indicates how many y-axis datapoints there willbe
  #V05 spaces out the table columns on the x-axis
  #V1 is a vector of all the columns that will be in the table
lab_left1D<-data.frame(
           V0 = factor(
                 c(as.factor(1:length(datD$authorREF2))),
                 levels=c(as.factor(length(datD$authorREF2):1))
                     ), 
           V05 = rep(
                  c(1,1.25,1.5,1.7),
                  each=length(datD$authorREF2)
                     ), 
           V1 = c(c(as.character(datD$authorREF2)),
                  c(as.character(datD$Events)),
                  c(as.character(datD$patients)),
                  c(as.character(datD$risk))))

data_table_left1D<-ggplot(lab_left1D, 
                        aes(x = V05, y = V0, 
                        label = format(V1, nsmall = 1))) +
                 geom_text(
                  aes(fontface = 
                        ifelse(
                          V0==as.character((length(datD$authorREF2)-1)),'bold.italic',
                          ifelse(V0=='1','bold',
                          ifelse(V0==as.character
                                       (length(datD$authorREF2)),'italic','plain')))),

                       size = ifelse(
                       lab_left1D$V0==as.character(length(datD$authorREF2)),0,2),                                   hjust=0, vjust=1.3) +                   theme_bw() +
                  theme(panel.grid.major = element_blank(), 
                     legend.position = "none",
                     panel.border = element_blank(),
                     #axis.line = element_line(colour = "black"), 
                     axis.text.x = element_text(colour="white"),#element_blank(),
                     axis.text.y = element_blank(), 
                     axis.ticks = element_line(colour="white"),#element_blank(),
                     plot.margin = unit(c(0.5,0,0,0), "lines")) +
                     labs(x="",y="") +
                  coord_cartesian(xlim=c(1,2.5))

grid.arrange(data_table_left1D, p2D,widths=c(5,3))

dev.off()
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
HanzMoleman
  • 33
  • 1
  • 6
  • 1
    It _sure_ looks like the ggplot on the left still has major grid lines set. We'd be able to truly help if posted the actual code vs just the theme bits. We have no idea where else you may have introduced the possibility for major grid lines to creep their way back in. – hrbrmstr May 01 '16 at 02:26
  • I don't think it is a gridline-- I think it has something to do with how R renders into quartz on a Mac-- – HanzMoleman May 01 '16 at 17:51
  • I use a Mac and that's _alot_ of really interestingly formatted code. Not sure if you've seen [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) before but it helps explain how to work in the R side of the SO Q&A forum. – hrbrmstr May 01 '16 at 20:15
  • Hi @HanzMoleman, I'm the author of the blog you reference. Funnily enough I found this post troubleshooting some grey lines on some other plot code (survival plot) from my blog. Glad you got there. I'll endeavour to tidy up the blog posts! – nzcoops Mar 22 '17 at 04:02

1 Answers1

1

A minimal reproducible example would be more helpful than a swath of interestingly formatted and arranged code.

library(ggplot2)
library(ggthemes)
library(gridExtra)

gg <- ggplot()
gg <- gg + geom_text(data=data.frame(x=1, y=1, label="label"),
                     aes(x, y, label=label))
gg <- gg + theme_map()
gg_left <- gg

gg <- ggplot()
gg <- gg + geom_point(data=mtcars, aes(mpg, wt))
gg <- gg + theme_bw()
gg_right <- gg

grid.arrange(gg_left, gg_right, ncol=2)

enter image description here

As you can see, I'm on a Mac and even left the comfort of RStudio to test that in R.app to generate a "pure Quartz window".

I'd work on re-organizing your code, get the core plots working, then applying the theme you want vs relying on cut & paste code.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • Thanks for the guidance on questions and advice on how to figure this out – HanzMoleman May 02 '16 at 15:52
  • Hrbrmstr-- you were bang on-- they are gridlines appearing because i didn't include panel.grid.minor=element_blank(). Your advice on how to systematically proceed to figure this out was a great learning point- thanks! – HanzMoleman May 26 '16 at 02:29