The mirror_ticks function not working for ggplot - r

I want to place ticks on all four sides of my graph. The way suggested for this is mirror_ticks.
library(ggplot2)
library(ggplotTicks)
sp6<-ggplot(Anna_Smooth, aes(y=log10(Prob2), x=log10(AvSize)))+
geom_point( data=Anna_Smooth, aes(y=log10(Prob2), x=log10(AvSize), color=PART) )+
guides( color=FALSE)
sp8<-sp6+ labs(x=expression(paste(log(s))))+
labs(y=expression(paste(log(P(s)))) )+
theme(axis.text.y = element_text(size=14),
axis.text.x = element_text(size=14),
axis.title.y = element_text(size=15),
axis.title.x = element_text(size=15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=2)
)
sp10<-mirror_ticks(sp10, allPanels=TRUE)
My output sp10 has no ticks on opposite panels, same result if I put allPanels=TRUE
Is there a fix? I am open to learn how one does this with theme settings?

As of ggplot2 version 2.2.0 (2016-11-11), the scale_x_continuous() and scale_y_continuous() can display a secondary axis which is positioned opposite to the primary axis and which can be controlled with the sec.axis argument.
This can be used to mirror the tick marks.
The OP hasn't provided reproducible data so we use the mpg dataset which comes with the ggplot2 package:
Chart without mirrored tick marks
library(ggplot2)
g1 <- ggplot(mpg, aes(log10(displ), log10(hwy))) +
geom_point() +
theme(
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 15),
axis.title.x = element_text(size = 15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 2
)
)
g1
Chart with secondary axes
g1 +
scale_x_continuous(sec.axis = dup_axis()) +
scale_y_continuous(sec.axis = dup_axis())
g1 +
scale_x_continuous(sec.axis = dup_axis(name = NULL)) +
scale_y_continuous(sec.axis = dup_axis(name = NULL))
g1 +
scale_x_continuous(sec.axis = dup_axis(name = NULL, labels = NULL)) +
scale_y_continuous(sec.axis = dup_axis(name = NULL, labels = NULL))
Mirrored tick marks with log10 scales
The secondary axes are also available with the scale_x_log10() and scale_x_log10() functions.
So, it can be avoided to use the log() function within the call to aes() but by specifying an appropriate log scale:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme(
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(size = 15),
axis.title.x = element_text(size = 15),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 2
)
) +
scale_x_log10(sec.axis = dup_axis(name = NULL, labels = NULL)) +
scale_y_log10(sec.axis = dup_axis(name = NULL, labels = NULL))

Related

Combine 2 plots (ggplot) but use one legend and colour scheme

I have created 2 plots and combined them as seen below but i now wish to plot this with one legend and the same colour scheme for both plots, does anyone know what changes i need to make this?
code used to create plots
#plotting actual from 1998-2020
mapplot_temp = ggplot(map_preds20, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = GDP_pdiff), colour = "black")
mapplot1 = mapplot_temp + scale_fill_gradient(name = 'GDP per capita change %', low =
'red', high = 'yellow', na.value = 'grey') +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, size = 20, face = "bold"))
#plotting predicted from 2021-2040
mapplot_temp2 = ggplot(map_preds40, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = GDP_pdiff), colour = "black")
mapplot2 = mapplot_temp2 + scale_fill_gradient(name = 'GDP per capita change % ($)', low = 'red', high = 'yellow', na.value = 'grey') +
ggtitle('Predicted GDP per capita change') +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, size = 20, face = "bold"))
#combine plots with one legend and same colour scheme
mapplot1 + mapplot2
plot

Make the point in the middle of each grid in ggplot2?

Here is the data.
df <- data.frame(x = LETTERS[1:5], y = LETTERS[6:10], z = rnorm(5))
Below are the codes to generate a point plot.
ggplot(df, aes(x, y, fill = z, size = z)) +
geom_point(shape = 21, stroke = 1, colour = 'black')
I'm wondering how to make each point into the middle of each grid. Something like this.
I found it worked by adding a geom_tile function.
ggplot(df, aes(x, y)) +
geom_tile(fill = NA, colour = 'black') +
geom_point(aes(fill = z, size = z), shape = 21, stroke = 1, colour = 'black') +
theme(panel.background = element_blank(),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.x = element_blank(),
axis.line.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.title = element_text(size = 12),
axis.text.y = element_text(size = 10, colour = 'black'),
axis.text.x = element_text(size = 10, colour = 'black'),
strip.background = element_blank(),
strip.text = element_blank())

R color scatter plot with more color gradiant

Hello I have Dataframe ploting scatter plot with color using other column values plot is looking fine but the colour values are start from 0-100 and most of the values are in between 50-100 so I wanted put more color to diffrentiat can any one suggest me how can I do with the R, I tried with viridis color it is also looking same 50-100 color is almost looking similar color
link for the data
https://drive.google.com/file/d/1EKhRwup3vUC3KVFOOh4XtKERIr8FQj3x/view?usp=sharing
code what I tried
df=read.table("test.txt",sep='\t', header=TRUE)
df = data.frame(df)
p=ggplot(df, aes(log(data1), log(data2)),cex=1.9)+
geom_point(aes(color =data3)) +
theme(legend.position = "top")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 20, face="bold"))
You can play with a customized color palette and scale_colour_gradientn like this:
library(RColorBrewer)
library(ggplot2)
#Data
df <- read.delim(file='test.txt',stringsAsFactors = F)
#Palette
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
sc <- scale_colour_gradientn(colours = myPalette(100))
#Plot
ggplot(df, aes(log(data1), log(data2)),cex=1.9)+
geom_point(aes(color =data3)) + sc +
theme(legend.position = "top")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 20, face="bold"))
Output:
If you want more color try this:
#Palette 2
sc2 <- scale_colour_gradientn(colours = rainbow(7))
#Plot
ggplot(df, aes(log(data1), log(data2)),cex=1.9)+
geom_point(aes(color =data3)) + sc2 +
theme(legend.position = "top")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 20, face="bold"))
Output:
Update2: With breaks you can define limits for the color scale:
#Plot 3
ggplot(df, aes(log(data1), log(data2),color=data3),cex=1.9)+
geom_point() +
scale_colour_gradientn(colours = rainbow(25),breaks = seq(0,100,by=5))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 12, face="bold"),
legend.text = element_text(size = 7, face="bold"))
Output:
Update 3: If you want to have different colors you can mix different palettes like this:
#Plot
ggplot(df, aes(log(data1), log(data2),color=data3),cex=1.9)+
geom_point() +
scale_colour_gradientn(colours = c(viridis::inferno(5),
viridis::plasma(5),
viridis::magma(5),
viridis::viridis(5),
rainbow(5)),breaks = seq(0,100,by=5))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 12, face="bold"),
legend.text = element_text(size = 7, face="bold"))
Output:

Use Linetypes and colors examples as axis label in ggplot2

I have a graph with four lines with two different linetypes (solid and dashed) and two colors. I want to display a table below the graph containing some information about each curve, e.g. :
The labels of the y-axis of this table are "S.2", "D.2",...
Is it possible to use labels (for the y-axis) that are examples of what the curve looks like instead of text?
Such as in the legend of the graph?
That is instead of "S.2" having a short blue dashed line?
Here is the code to produce the plot
require(ggplot2)
require(grid)
require(gtable)
## graph
g1<-ggplot() + aes(x=seq(0,5,.1),y=cumsum(sample(0:10,51,replace = T)),colour="1",linetype="S") + geom_line()+
geom_line(aes(x=seq(0,5,.1),y=cumsum(sample(0:10,51,replace = T)),colour="1",linetype="D"))+
geom_line(aes(x=seq(0,5,.1),y=cumsum(sample(0:10,51,replace = T)),colour="2",linetype="S"))+
geom_line(aes(x=seq(0,5,.1),y=cumsum(sample(0:10,51,replace = T)),colour="2",linetype="D"))+
scale_colour_manual(breaks=c("1","2"),values=c("#cd7118","#1874cd"))+
scale_x_continuous(limits = c(0,5),breaks = 0:5,expand=c(0,0))+
scale_linetype_manual(breaks=c("S","D"),values=c("solid","dashed"))+
theme(
axis.line = element_line(size = 0.2, linetype = 'solid',colour = "black"),
axis.text = element_text(size=10),
axis.title = element_blank(),
legend.title = element_blank(),
legend.background = element_blank(),
legend.key = element_blank(),
legend.position = c(.1,.5)
)
## table
count<-data.frame(time=rep(0:5,4),count=rep(0:5,4),col=c(rep("1",12),rep("2",12)),lintype=rep(c(rep("S",6),rep("D",6)),2))
g2<-ggplot(count, aes(x = time , y = interaction(factor(lintype),factor(col)), colour=factor(col), label=count)) +
geom_text(size=4) +
scale_colour_manual(breaks=c("1","2"),values=c("#cd7118","#1874cd"))+
scale_x_continuous(limits = c(0,5),breaks = 0:5,expand=c(0,0))+
theme(
legend.position = "none",
panel.background = element_rect(fill = "white",colour = "white",size = 0.5, linetype = "solid"),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(size=10, color = 'black',margin = margin(t = 0, r = 20, b = 0, l = 0)),
axis.ticks=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(size=10),
plot.margin = margin(t=1,b=1,r=15,l=10,unit="pt")
)
#
merge = rbind(ggplotGrob(g1), ggplotGrob(g2), size="last")
panels <- merge$layout$t[grep("panel", merge$layout$name)]
merge$heights[panels[1]] <- unit(10,"cm")
merge$heights[panels[2]]<-unit(6,"lines")
merge <- gtable_add_rows(merge, heights = unit(1,"line"), 1)
merge$layout$clip[merge$layout$name=="panel"]<-"off"
grid.draw(merge)

How to add a custom legend for geom_hline

Using the data below, notice the legend does not properly describe geom_hline:
df<-data.frame(
points=c(.153,.144,.126,.035, .037, .039, .010,.015,.07),
days=gl(3,1,9,labels=c("Sun","Mon","Tues")),
lang=c("en","en","en","pt","pt","pt","ko","ko","ko"))
ggplot(data=df[df$lang=="en",])+
geom_point(aes(x=days,y=points),size=5,colour='cyan',show_guide=F)+
geom_point(aes(x=days,y=points,colour=days),size=4,show_guide=F)+
facet_wrap(~lang,ncol=1,scales="free")+
xlab("")+
ylab("")+
scale_y_continuous(labels = percent_format())+
theme(legend.position="right",
legend.title = element_blank(),
strip.text.x = element_text(size = 13, colour = 'black', angle = 0),
axis.text.x=element_text(angle=0, hjust=.5, vjust=0),
legend.position = 'none',
panel.background = element_rect(fill = "#545454"),
panel.grid.major = element_line(colour = "#757575"),
panel.grid.minor = element_line(colour = "#757575"))+
geom_hline(yintercept=.136,color='cyan',size=2, show_guide=T)
Is there some way to create a custom legend that describes the geom_hline? [I'd like the legend to to have the name "Legend" with only one value labled "avg"]
Searching so, I found an example: ggplot legend showing transparency and fill color. They used scale_fill_manual, which I tried, but could not improve the legend displayed above.
It's a bit awkward, but this seems to work:
hline <- data.frame(yint = 0.136,lt = 'Avg')
ggplot(data=df[df$lang=="en",])+
geom_point(aes(x=days,y=points),size=5,colour='cyan')+
geom_point(aes(x=days,y=points,colour=days),size=4)+
facet_wrap(~lang,ncol=1,scales="free")+
xlab("")+
ylab("")+
scale_y_continuous(labels = percent_format())+
theme(legend.position="right",
legend.title = element_blank(),
strip.text.x = element_text(size = 13, colour = 'black', angle = 0),
axis.text.x=element_text(angle=0, hjust=.5, vjust=0),
legend.position = 'none',
panel.background = element_rect(fill = "#545454"),
panel.grid.major = element_line(colour = "#757575"),
panel.grid.minor = element_line(colour = "#757575"))+
geom_hline(data = hline,aes(yintercept=yint,linetype = lt),color = "cyan",size=2,show_guide = TRUE) +
scale_colour_discrete(guide = "none") +
scale_linetype_manual(name = 'Legend',values = 1,guide = "legend")
Ah, but you've included legend.title = element_blank() in there, which is why the legend is not named. Remove that to include the name.

Resources