1

I have a .csv file, test1.csv that looks like this:

seed,rate,TYPE,SUFFIX
1,1,A,Sim
1,1,A,Ana
2,1,A,Ana
2,2,A,Ana

1,1,B,Sim
1,1,B,Ana
2,1,B,Ana
2,2,B,Ana

1,1,C,Sim
2,2,C,Ana

I want to set alpha = 1 for rate = 1 and alpha = 0 for rate = 2

I have the following R code:

require(ggplot2)
require(scales)
require(dplyr)


pdf(file="sweep_feasibility-vs-injection_test.pdf", height=3, width=6)

a<-read.csv('./test1.csv',header=T);
a$alphayr <- as.factor(ifelse(a$rate == 1, TRUE, FALSE))

a<-na.omit(a)
p<-ggplot(a,aes(x=rate,group=factor(SUFFIX))) +
    geom_bar(stat="count", position = "dodge", aes(fill=factor(SUFFIX), alpha=alphayr))+
        scale_alpha_manual(values = c(0,1), guide = F) +
    facet_grid(~TYPE)+
    xlab('Injection Rate (%)') +
    ylab('Feasible (%)');

p + theme_bw() %+replace% theme(axis.title=element_text(),axis.title.y=theme_bw()$axis.title.y) +
    theme(
      axis.line=element_line(color='black'),
      legend.position="top",
      legend.background=element_rect(fill="transparent"),
      axis.title.x = element_text(size=15),
      axis.title.y = element_text(size=15),
      panel.border=element_blank(),
      panel.grid.minor = element_blank(),
      panel.grid.major.x = element_blank(),
      legend.key.width=unit(0.5,"cm"),
      legend.key.height=unit(0.5,"cm"),
      plot.title = element_text(hjust = 0.5),
      legend.key = element_rect(colour = "black", size=0.1),
      legend.title=element_text(size=10),
      axis.text.y=element_text(size=15,color='black'),
      axis.text.x=element_text(angle=0,hjust=0.5, size=15, color='black'),
      legend.text=element_text(size=15))+
guides(
       fill=guide_legend(nrow=1,title=""),
       color=guide_legend(nrow=1,title=""),
       shape=guide_legend(nrow=1,title="")
       );

when I run this code, I get the following graph:

enter image description here

I don't understand why I am seeing a bar for rate = 2

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Tushar
  • 415
  • 2
  • 16
  • 1
    Could you please edit your code to make it minimal and reproducible? It's hard to pick out the code relevant to your question about alpha amidst all of the irrelevant theme edits. See [how to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [mcve] – Jan Boyer Feb 08 '19 at 16:20
  • It is reproducible. I am running it on Linux. and I have the data in test1.csv given at the top. – Tushar Feb 08 '19 at 16:21
  • Why do you load all this packages if you only need to use `ggplot2`? – NelsonGon Feb 08 '19 at 16:24
  • actually I have other data in my R code that I haven't shared. That data require those packages but for this problem they are irrelevant. You are right, I should have deleted the packages. – Tushar Feb 08 '19 at 16:25

1 Answers1

2

The cause of this is group = factor(SUFFIX) which, I think, isn't really a relevant aesthetic for geom_bar at all. Erasing that gives

enter image description here

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102