Disable y axis scale and labels - r

I have a plot in ggplot:
ggplot(plot1,aes(x=c,y=value,colour=variable,linetype=variable,size=variable)) +
geom_line() +
scale_x_continuous(breaks=seq(1,10,1)) +
scale_y_continuous(breaks=seq(0,1, 0.1))+
scale_colour_manual(values=rep(c("blue3","red3"),each=9)) +
scale_linetype_manual(values = rep(c(3,1),each=9)) +
scale_size_manual(values = rep(c(0.6,0.3),each=9)) +
theme_bw(base_size=12, base_family="Helvetica") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8),
text = element_text(size=14),
axis.line = element_line(size=0.25),
axis.ticks=element_line(size=0.25),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position="none" ,
legend.direction="vertical",
legend.title=element_blank(),
legend.text=element_text(size=8),
legend.background=element_blank(),
legend.key=element_blank())+labs(title='a',x='b', y='c' )+theme(plot.title=element_text(size=8))+theme(plot.margin=unit(c(0.1,0,0.1,0),"cm"))
I would like to disable:
the numbers on the y axis
the small lines indicating the position of the numbers
the label
Is this possible to do without affecting the x axis?

I think you can use this approach.
p1 + theme(axis.text.y=element_blank())+ylab("") + scale_y_discrete(breaks=NULL)
# p1 = your initial graph
# axis.text.y removes number
# ylab("") removes y label
# scale_y_discrete(breaks=NULL) removes the tick marks. Your x-axis will not be affected.
Good luck.

You can also do everything in the theme() part of your code. Just add the following code:
p1 + theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())

Related

R: overlapping tooltips when hovering in ggplotly

I have this code. Basically, when mousing over the ggplotly plot in the value=50, I have two labels overlapping each other so basically I cannot see the West label, only the South.
How can I prevent that from happening? What am I doing wrong? I would like to see both labels separated when mousing over.
library(ggplot2)
library(ggplotly)
data <- data.frame(
name=c( "A" ),
value=c( 30,40,50,50),
location=c("North","East", "West","South")
)
pxp<- ggplot(data, aes(x=name, y=value, text=location)) + geom_boxplot() +geom_point() +
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+
labs(y="Value")
ggplotly(pxp)
Thank you,
You can use jitter and give appropriate width and height to see the points separately. Then it displays the appropriate values when you hover. Try this
pxp<- ggplot(data, aes(x=name, y=value, text=location)) + geom_boxplot() + # geom_point(position=jitter, width=0.1) +
geom_jitter(alpha=0.6, width=0.02, height=0.1)+
theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+
labs(y="Value")
ggplotly(pxp)

When using identity input, how do you get ggplot to display % symbol next to value label

The below produces a plot with value labels - from a table containing percentages; NOT raw individual level data. So the stat="identity" argument is used. How would you instruct ggplot to add a % symbol to each value label above the bar in such a case?
#Generate data table
Percent<-c(20,80)
row_labels<-c("Male","Female")
df<-data.frame(row_labels,Percent)
#Plot data table
ggplot(df) + aes(row_labels,Percent,fill=row_labels) + geom_bar(stat="identity") +
labs(x = "",title = " ") + theme(legend.title = element_blank()) + coord_flip()+ theme(legend.position="none") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_blank(), axis.ticks.y = element_blank(), axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank()) + geom_text(aes(label=round(Percent,digits=1)), hjust = -0.15, size = 3)
I have modified aes of the geom_text in the following way:
ggplot(df, aes(row_labels,Percent,fill=row_labels) ) +
geom_bar(stat="identity") +
geom_text(aes(y=Percent+5, label = paste(Percent, "%")) ) +
labs(x = "",title = " ") +
theme(
legend.position="none",
legend.title = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_blank(),
axis.ticks.y = element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank()
) +
coord_flip()
Although the order of layers/adjustments does not matter (you have mentioned coord_flip), I have reorganised the code for better fluency.

Barplot with both stacked and grouped options, without face grid

I am trying to make a stacked and grouped barplot using the following datasets:
dfplot <- data.frame(organisms=c("M.musculus","D.melanogaster" ,"H.sapiens","O.sativa","S.pombe","C.familiaris",
"G.gallus","P.falciparum","A.thaliana","C.elegans","D.rerio","B.taurus","S.cerevisiae","R.norvegicus","C.intestinalis","B.subtilis","E.coli"),
KEGGv2=c(20,7,21,126,106,62,26,80,5,5,13,306,8,35,32,104,107),
KEGGv1=c(286,124,289,0,0,244,135,0,121,124,148,0,101,271,87,0,0),
Reactome=c(358,146,596,115,54,306,370,23,155,112,365,341,52,364,0,0,0))
# Melt the dataframe
melted <- melt(dfplot, "organisms")
# Reformat the data labels
melted$cat <- ''
melted[melted$variable == 'Reactome',]$cat <- "Reactome"
melted[melted$variable != 'Reactome',]$cat <- "KEGG"
ggplot(melted, aes(x=cat, y=value, fill=variable)) +
geom_bar(stat = 'identity', position = 'stack') +
facet_grid(~ organisms)+
scale_fill_manual(values=c("deepskyblue4", "lightblue1", "olivedrab2")) +
labs(y = "Number of Pathways") +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.title=element_blank(),
strip.text.x = element_blank(),
strip.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"))
And my output is the following one:
I used a facet_grid per organism because I wanted to have a mix between stack (for both KEGGv1 and KEGGv2) and then group it with the other group Reactome. However, when doing the melting, the x-axis will have two labels per facet_grid (KEGG and Reactome, which is the variable obtained from the melting [cat]). I did not want that label so I remove them.
What I am trying to do is to have the organism name per each of the groupings, so each of the 17 grouping bars will have the name of the corresponding organism**. Therefore, I am missing that, I have tried several ways but I cannot find the proper way of doing it.
Thanks in advance,
You can do two things to make this work:
Rotate the labels of the facet so that they plot at 90 degrees
Plot the facet labels beneath the graph using the switch = 'x' argument within facet_grid.
Here is the complete example:
ggplot(melted, aes(x=cat, y=value, fill=variable)) +
geom_bar(stat = 'identity', position = 'stack') +
facet_grid(~organisms, switch = 'x')+
scale_fill_manual(values=c("deepskyblue4", "lightblue1", "olivedrab2")) +
labs(y = "Number of Pathways") +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.title=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line.y = element_line(colour = "black"),
strip.text = element_text(angle = 90))
I left the default shaded background, but you can easily style as you prefer.
Aproach Two
You might want to consider rotating the plot. This requires you to flip the coordinates of the plot using coord_flip before the facet is called. As we are faceting in the other direction we change the facet argument to facet_grid(organisms~.). All other arguments which refer to x or y are swapped over:
ggplot(melted, aes(x=cat, y=value, fill=variable)) +
geom_bar(stat = 'identity', position = 'stack') +
coord_flip() +
facet_grid(organisms~., switch = 'y') +
scale_fill_manual(values=c("deepskyblue4", "lightblue1", "olivedrab2")) +
labs(y = "Number of Pathways") +
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
legend.title=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(colour = "black"),
strip.text.y = element_text(angle = 180))
First you can set the organism labels to be below the bars, rather than above. This is established by setting the switch argument of the facet_grid function to "x" (see code below). Secondly, you should then remove axis.title.x=element_blank() argument within your theme function call. The updated code is below. Hope this helps!
ggplot(melted, aes(x = cat, y = value, fill =variable)) +
geom_bar(stat = 'identity', position = 'stack') +facet_grid(~organisms, switch="x")+
scale_fill_manual(values=c("deepskyblue4","lightblue1","olivedrab2"))+
ylab("Number of pathways")+
theme(axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank())+theme(legend.title=element_blank())+
theme(strip.background = element_blank())+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black"))

Can you get the axis of the marginal densities to line up with the axis of the scatter plot

Hi I have the plot below and the marginal density plots are slightly off. They do not line up to the x and y axis of the scatter plot so interpretation can be a bit misleading.
I can sort of play with these lines of code to try and get the margins to align for rthe marginal plots but it is very manual and frustrating.
theme0(plot.margin = unit(c(1,0,0,2.2),"lines"))
theme0(plot.margin = unit(c(0,1,1.2,0),"lines"))
Is there a way to automatically find the right margins to pass to theme0(plot.margin = unit(c(0,1,1.2,0),"lines") so that no manual work needs to be done to line up the margins? Thank you.
library(ggplot2)
library(gridExtra)
set.seed(42)
DF <- data.frame(x=rnorm(100,mean=c(1,5)),y=rlnorm(100,meanlog=c(8,6)),group=1:2)
DF
## Scatter plot
p1 <- ggplot(DF,aes(x=x,y=y)) + geom_point() +
scale_x_continuous(expand=c(0.02,0)) +
scale_y_continuous(expand=c(0.02,0)) +
theme_bw() +
theme(legend.position="none",plot.margin=unit(c(0,0,0,0),"points")) # ggplot(DF,aes(x=x,y=y,colour=factor(group))) color the gorup
theme0 <- function(...) theme( legend.position = "none",
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.margin = unit(0,"null"),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.length = unit(0,"null"),
axis.ticks.margin = unit(0,"null"),
panel.border=element_rect(color=NA),...)
### DENSITY OF X
p2 <- ggplot(DF,aes(x=x, fill="blue")) +
geom_density(alpha=0.5) +
scale_x_continuous(breaks=NULL,expand=c(0.02,0)) +
scale_y_continuous(breaks=NULL,expand=c(0.02,0)) +
theme_bw() +
theme0(plot.margin = unit(c(1,0,0,2.2),"lines")) # to color group ggplot(DF,aes(x=x,colour=factor(group),fill=factor(group)))
### DENSITY OF Y
p3 <- ggplot(DF,aes(x=y, fill = "red")) +
geom_density(alpha=0.5) +
coord_flip() +
scale_x_continuous(labels = NULL,breaks=NULL,expand=c(0.02,0)) +
scale_y_continuous(labels = NULL,breaks=NULL,expand=c(0.02,0)) +
theme_bw() +
theme0(plot.margin = unit(c(0,1,1.2,0),"lines")) # color group ggplot(DF,aes(x=y,colour=factor(group),fill=factor(group)))
grid.arrange(arrangeGrob(p2,ncol=2,widths=c(3,1)),
arrangeGrob(p1,p3,ncol=2,widths=c(3,1)),
heights=c(1,3))

ggplot2 theme with no axes or grid

I am trying to make a plot with no information beyond the data. No axes; no grid; no title; just the plot.
But I keep getting extra margins and padding that I can't remove.
library(ggplot2)
library(grid)
theme_bare <- theme(
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
#axis.ticks.length = unit(0, "lines"), # Error
axis.ticks.margin = unit(c(0,0,0,0), "lines"),
legend.position = "none",
panel.background = element_rect(fill = "gray"),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.margin = unit(c(0,0,0,0), "lines"),
plot.background = element_rect(fill = "blue"),
plot.margin = unit(c(0,0,0,0), "lines")
)
ggplot() +
geom_area (data=economics, aes(x = date, y = unemploy), linetype=0) +
theme_bare
Produces this image:
What I want is this:
I can't figure out how to get rid of the blue and make the dark gray flush with the edges.
Could any one offer some advice?
Here is the way to plot only the panel region:
p <- ggplot() + geom_area (data=economics, aes(x = date, y = unemploy), linetype=0) +
scale_x_date(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) +
theme(line = element_blank(),
text = element_blank(),
title = element_blank())
gt <- ggplot_gtable(ggplot_build(p))
ge <- subset(gt$layout, name == "panel")
grid.draw(gt[ge$t:ge$b, ge$l:ge$r])
From ggplot2_2.0.0 you can use theme_void:
ggplot() +
geom_area(data = economics, aes(x = date, y = unemploy), linetype = 0) +
theme_void()
try
last_plot() + theme(axis.ticks.length = unit(0.001, "mm")) + labs(x=NULL, y=NULL)
you may want to file a bug for the 0 tick length.
If you just want to remove the grid in theme_bw(), you can use:
+ theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

Resources