How to separate aesthetics of two different geom_lines? - r

I'm trying to plot a liine on the x axis which is basically a bunch of zeros and ones. Ones are green and zeros are red. When I try to do that, the color_scale_gradient of the ggplot basically colors on top of the line.
It looks like this
Where the line should be colored as follows:
colorbar is a vector of zeros and ones.
p <- ggplot(data1,aes(newx,newy, group = 1, colour=newy))+
geom_line(size=1.5, show.legend = FALSE)+
scale_colour_gradient(low="red2", high="green3") +
geom_line(data = colorFrame, aes(as.numeric(x)-5,as.numeric(ys), color = colorbar),size=3, show.legend = FALSE)+
xlim(0,1300)
p <- p +
theme(panel.background = element_blank(), axis.ticks.x = element_blank(),
axis.text.x = element_blank(), axis.line.y = element_line(colour = 'black'),
axis.ticks.y.left = element_line(colour = 'black')) +
scale_y_continuous(breaks = seq(0, 12, 1), limits = c(-1, 12), expand = c(0,0))

One solution would be to create two subplots and stitch them together. I use cowplot and theme_void here, but really the second plot below could look however you want it to.
p1 <- ggplot(df, aes(x,y, group = 1, colour=y)) +
geom_line(size=1.5, show.legend = FALSE) +
scale_colour_gradient(low="red2", high="green3") +
theme(panel.background = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.line.y = element_line(colour = 'black'),
axis.ticks.y.left = element_line(colour = 'black')) +
scale_y_continuous(breaks = seq(0, 12, 1), limits = c(-1, 12), expand = c(0,0)) +
labs(x = NULL)
p2 <- ggplot(df, aes(x, y = 0, colour=z)) +
geom_line(size=1.5, show.legend = FALSE) +
scale_colour_gradient(low="red2", high="green3") +
theme_void()
cowplot::plot_grid(p1, p2,
ncol = 1,
rel_heights = c(1, .05),
align = 'v')
Data
df <- data.frame(x = 1:50,
y = runif(50, 0, 12),
z = sample(c(0,1), 50, replace = TRUE))

Related

How to remove unwanted horizontal lines added to raster image by ggsave?

I can't solve a problem I found when plotting and saving an image from raster with the ggsave() function in R.
When I plot it, it works well. When I use ggsave() to export it, horizontal gray lines are added to the plot.
I want to remove them but I don't know how to do it.
That's an example image with the options and the code I used:
gg.opzioni = list(geom_tile(aes(x, y, fill = values)),
scale_fill_gradientn(n.breaks = 3, colours = c("#52647A", "#2C413C", "#646859"), guide = "legend", na.value = "white"),
theme(plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(size = 12), axis.title.y = element_text(size = 12),
plot.margin = unit(c(2, 2, 2, 2), "mm"), panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill = NA, size = 1),
axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.text.y = element_blank(),
axis.ticks.y = element_blank(), panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank(),
panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank(), aspect.ratio = 11/10),
scale_x_continuous(limits = c(0, 1), expand = c(0, 0), breaks = seq(0, 1, 0.1), labels = seq(0, 10, 1)),
scale_y_continuous(limits = c(0, 1), expand = c(0, 0), breaks = seq(0, 1, 0.1), labels = seq(0, 10, 1)),
coord_fixed())
r.sam = ggplot(df) + gg.opzioni + labs(title = "Campione ricostruito", x = "", y = "", fill = "classe:")
ggsave(filename = "lapalma_sam.png", plot = r.sam, device = "png", path = "/Users/Francesco/Downloads/")
I tried to remove the possible grid with the panel.grid options, but it didn't work.
Originally three variables are included in the df object: two of coordinates and one with the pixel class.
library(tidyverse)
df <- tibble(
val = rep(sin(seq(0, 4*pi, length = 100)), 100),
x = rep(1:100, 100),
y = rep(1:100, each = 100)
)
The following replicates your problem, where horizontal lines are visible around each cell:
plot.tiles <- ggplot(data = df, aes(x = x, y = y, fill = val)) +
geom_tile()
ggsave('plot_tile.png', plot.tiles)
This arises because geom_tile() has a border color property. One solution is to make the "color" aesthetic match the "fill" aesthetic:
plot.border <- ggplot(data = df, aes(x = x, y = y, fill = val, color = val)) +
geom_tile()
ggsave('plot_border.png', plot.border)
Or you can use geom_raster(), which does not have a cell border, but functions similarly to geom_tile():
plot.raster <- ggplot(data = df, aes(x = x, y = y, fill = val)) +
geom_raster()
ggsave('plot_raster.png', plot.raster)

Change histogram bar percentage label in R ggplot

I have made a histogram in R using the following code:
(I have tried generating a reprex. Try the code reprex here
progressiveNumber = c(1:50)
c = c(-0.22037439, -0.21536365, -0.34203720, 0.04501624, -0.13141665, -1.28155157, -0.08394700, -0.08484768, -0.12577287, 0.30402612, -0.40578251,
0.00000000, -0.16849942, -0.04212114, 0.12577287, 0.57366312, -0.84766743, -1.03909659, -0.21536365, -0.46263648, -0.48181028, -0.38887381,
-0.38571106, -0.38571106, -0.26220026, 0.73227348, -0.38887381, -0.96590662, -0.29931065, 0.04272655, 0.04182587, -0.38571106, -0.13141665,
-0.34614726, -0.49063020, -0.08484768, 0.05249378, 0.08484768, -0.74591104, 0.46263648, -0.42081062, 0.00000000, 0.08394700, -0.38571106,
-0.34203720, -0.04212114, -0.79517364, 0.25429442, -0.30402612, -0.08365173)
library(tidyverse)
# DEFINING BREAKS AND CUT A VECTOR INTO BINS
# set up cut-off values
breaks <- c(-1.2816,-0.3881,-0.2154, 0.0000, 0.3 ,0.7323)
# specify interval/bin labels
tags <- c("[-1.2 / -0.3]","[-0.3 / -0.2]", "[-0.2 / 0]", "[0 / 0.3]","[0.3 / 0.7]")
# bucketing values into bins
group_tags <- cut(c,
breaks=breaks,
include.lowest=TRUE,
right=FALSE,
labels=tags)
# inspect bins
summary(group_tags)
# c_groups <- factor(group_tags,levels = labels, ordered = TRUE) # this line doesn't work for some reason
#tiff("percentageBinsC.tiff", units="in", width=5, height=5, res=300,)
p2 = ggplot(data = as_tibble(group_tags), mapping = aes(x=value)) +
geom_bar(fill="deepskyblue1",color="white",alpha=0.7, ) +
stat_count(geom="text", aes(label=sprintf("%.2f",..count../length(group_tags))), vjust=-0.5) +
labs(y = 'Count', x='C') +
theme(text = element_text(size=20), axis.line.x = element_line(color = "black", size = 1),
axis.line.y = element_line(color = "black", size = 1), axis.text.x = element_text(angle = 35, hjust = 1, vjust = 1),
panel.background = element_blank(), panel.border = element_blank(),
panel.grid.minor = element_blank(),panel.grid.major = element_blank())
p2
#dev.off()
Result
I would like to change the label on the bars (not the x-axis label but the ones that are right on top of each bar) from, e.g., 0.26 to 26%, 22% and so on.
How can I do that?
You can use percent_format from scales, first we define a function to do the conversion, and the rounding up you did with sprintf:
convert2perc = scales::percent_format(accuracy = 2)
You can test it:
convert2perc(0.107)
[1] "10%"
Then use it in the plotting:
p2 = ggplot(data = as_tibble(group_tags), mapping = aes(x=value)) +
geom_bar(fill="deepskyblue1",color="white",alpha=0.7, ) +
stat_count(geom="text", aes(label=convert2perc(..count../length(group_tags))), vjust=-0.5) +
labs(y = 'Count', x='C') +
theme(text = element_text(size=20), axis.line.x = element_line(color = "black", size = 1),
axis.line.y = element_line(color = "black", size = 1), axis.text.x = element_text(angle = 35, hjust = 1, vjust = 1),
panel.background = element_blank(), panel.border = element_blank(),
panel.grid.minor = element_blank(),panel.grid.major = element_blank())

Is it possible to avoid axis label overlapping by ggrepel?

I am drawing heatmap with ggplot2. Several ticks on y axis need to be labeled. However,some of them are too close and overlap. I know ggrepel could separate text labels, but currently I have not worked out for my problem.
My code is as following. Any suggestion is welcome. Thanks.
Code:
df <- data.frame()
for (i in 1:50){
tmp_df <- data.frame(cell=paste0("cell", i),
gene=paste0("gene", 1:100), exp = rnorm(100), ident = i %% 5)
df<-rbind(df, tmp_df)
}
labelRow=rep("", 100)
for (i in c(2, 5, 7, 11, 19, 23)){
labelRow[i] <- paste0("gene", i)
}
library(ggplot2)
heatmap <- ggplot(data = df, mapping = aes(x = cell, y = gene, fill = exp)) +
geom_tile() +
scale_fill_gradient2(name = "Expression") +
scale_y_discrete(position = "right", labels = labelRow) +
facet_grid(facets = ~ident,
drop = TRUE,
space = "free",
scales = "free", switch = "x") +
scale_x_discrete(expand = c(0, 0), drop = TRUE) +
theme(axis.line = element_blank(),
axis.ticks = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_text(),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
strip.text.x = element_text(angle = -90))
heatmap
For these kinds of problems, I prefer to draw the axis as a separate plot and then combine. It takes a bit of fiddling but allows you to draw pretty much any axis you want.
In my solution, I'm using the functions get_legend(), align_plots(), and plot_grid() from the cowplot package. Disclaimer: I'm the package author.
library(ggplot2)
library(cowplot); theme_set(theme_gray()) # undo cowplot theme setting
library(ggrepel)
df<-data.frame()
for (i in 1:50){
tmp_df <- data.frame(cell=paste0("cell", i),
gene=paste0("gene", 1:100), exp=rnorm(100), ident=i%%5)
df<-rbind(df, tmp_df)
}
labelRow <- rep("", 100)
genes <- c(2, 5, 7, 11, 19, 23)
labelRow[genes] <- paste0("gene ", genes)
# make the heatmap plot
heatmap <- ggplot(data = df, mapping = aes(x = cell,y = gene, fill = exp)) +
geom_tile() +
scale_fill_gradient2(name = "Expression") +
scale_x_discrete(expand = c(0, 0), drop = TRUE) +
facet_grid(facets = ~ident,
drop = TRUE,
space = "free",
scales = "free", switch = "x") +
theme(axis.line = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
strip.text.x = element_text(angle = -90),
legend.justification = "left",
plot.margin = margin(5.5, 0, 5.5, 5.5, "pt"))
# make the axis plot
axis <- ggplot(data.frame(y = 1:100,
gene = labelRow),
aes(x = 0, y = y, label = gene)) +
geom_text_repel(min.segment.length = grid::unit(0, "pt"),
color = "grey30", ## ggplot2 theme_grey() axis text
size = 0.8*11/.pt ## ggplot2 theme_grey() axis text
) +
scale_x_continuous(limits = c(0, 1), expand = c(0, 0),
breaks = NULL, labels = NULL, name = NULL) +
scale_y_continuous(limits = c(0.5, 100.5), expand = c(0, 0),
breaks = NULL, labels = NULL, name = NULL) +
theme(panel.background = element_blank(),
plot.margin = margin(0, 0, 0, 0, "pt"))
# align and combine
aligned <- align_plots(heatmap + theme(legend.position = "none"), axis, align = "h", axis = "tb")
aligned <- append(aligned, list(get_legend(heatmap)))
plot_grid(plotlist = aligned, nrow = 1, rel_widths = c(5, .5, .7))

Prevent geom_points and their corresponding labels from overlapping

Thanks for the suggested duplicate, this is however not only about the labels, but is also about adjusting the points themselves so they do not overlap.
have a quick look at the plot below...
I need the coloured points, and their corresponding labels, to never overlap. They should be clustered together and all visible, perhaps with some indication that they are spaced and not 100% accurate, perhaps some sort of call out? Open to suggestions on that.
I've tried adding position = 'jitter' to both geom_point and geom_text, but that doesn't seem to be working (assume it is only for small overlaps?)
Ideas?
# TEST DATA
srvc_data <- data.frame(
Key = 1:20,
X = sample(40:80, 20, replace = T),
Y = sample(30:65, 20, replace = T)
)
srvc_data$Z <- with(srvc_data,abs(X-Y))
t1<-theme(
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(size=.4)
)
main_plot <- ggplot(srvc_data, aes(x = X, y = Y),xlim=c(0,100), ylim=c(0,100)) +
t1 +
theme_bw() +
labs(x="X", y="Y") +
scale_x_continuous(limits = c(0, 100)) +
scale_y_continuous(limits = c(0, 100)) +
geom_abline(intercept = 0, slope = 1, colour="blue", size=34, alpha=.1)+
geom_abline(intercept = 0, slope = 1, colour="black", size=.2, alpha=.5,linetype="dashed")+
geom_point(size = 7, aes(color = Z), alpha=.7) +
scale_color_gradient("Gap %\n",low="green", high="red")+
coord_fixed()+
geom_text(aes(label=Key,size=6),show_guide = FALSE)
main_plot
Produces this plot (of course with your random data it will vary)
Thanks in advance.
Here's your plot with ggrepel geom_text_repel:
library(ggrepel)
# TEST DATA
set.seed(42)
srvc_data <- data.frame(
Key = 1:20,
X = sample(40:80, 20, replace = T),
Y = sample(30:65, 20, replace = T)
)
srvc_data$Z <- with(srvc_data,abs(X-Y))
t1<-theme(
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(size=.4)
)
ggplot(srvc_data, aes(x = X, y = Y),xlim=c(0,100), ylim=c(0,100)) +
t1 +
theme_bw() +
labs(x="X", y="Y") +
scale_x_continuous(limits = c(0, 100)) +
scale_y_continuous(limits = c(0, 100)) +
geom_abline(intercept = 0, slope = 1, colour="blue", size=34, alpha=.1)+
geom_abline(intercept = 0, slope = 1, colour="black", size=.2, alpha=.5,linetype="dashed")+
geom_point(size = 7, aes(color = Z), alpha=.7) +
scale_color_gradient("Gap %\n",low="green", high="red")+
coord_fixed()+
geom_text_repel(aes(label=Key,size=6),show_guide = FALSE)

Add legend to ggplot object (why two legends?)

I created a ggplot2 object:
a <- replicate(8,rnorm(100))
colnames(a) <- letters[1:8]
b < -melt(a,id.vars=1:1)
colnames(b) <- c("c","variable","value")
ggplot(b,aes(x = c,y = value, colour = variable, linetype = variable)) +
geom_line()+
geom_point(aes(shape = factor(variable)), size = 1.7) +
scale_x_continuous(limits = c(-1, 1),
breaks = seq(-1, 1, 0.1),
expand=c(0.01, 0.01)) +
scale_y_continuous(limits = c(-1, 1),
breaks = seq(-1, 1, 0.1),
expand = c(0.01, 0.01))+
theme_bw(base_size = 12, base_family = "Helvetica") +
theme(axis.text=element_text(size = 10),
axis.title=element_text(size = 10),
text = element_text(size = 10),
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_rect(colour = "black", fill = NA, size = 0.5),
panel.background = element_blank(),
legend.position = "top" ,
legend.direction = "vertical",
legend.title = element_blank(),
legend.text = element_text(size = 13),
legend.background = element_blank(),
legend.key = element_blank()) +
labs(x = '', y = '', title = "") +
theme(plot.title = element_text(size=10)) +
theme(strip.text.x = element_text(size = 8,color="black"),
strip.background = element_blank()) +
theme(strip.text.x = element_text(size = 8, colour = "black"))
My problem is the following:
when I create the legend, there is a separate legend for the colors and a separate one for the points.
How can I create a single legend for each of the 8 variables?
Let me minimise your code and focus on the legend issue. This is what you have now.
ggplot(b,aes(x = c, y = value, colour = variable, linetype = variable)) +
geom_line() +
geom_point(aes(shape = factor(variable)),size=1.7)
Your data frame, b has variable as factor. You use this in two ways here; variable and factor(variable). You can simply use variable for shape in geom_point; make all variable identical.
ggplot(b,aes(x = c, y = value, colour = variable, linetype = variable)) +
geom_line()+
geom_point(aes(shape = variable),size = 1.7)
I saw some warning messages related to colours and other things. You may want to take care of them. But, for legend, this is one way to go.
Take from the ideas on this page: http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#modifying-the-text-of-legend-titles-and-labels
I edited your code to make the data visible (you had problems with your x-axis limits. Note the final three lines. These commands tell ggplot to create only one legend.
a<-replicate(6,rnorm(100))
colnames(a)<-letters[1:6]
b<-melt(a,id.vars=1:1)
colnames(b)<-c("c","variable","value")
ggplot(b,aes(x=c,y=value,colour=variable,linetype=variable)) +
geom_line() + geom_point(aes(shape=factor(variable)),size=1.7)+
scale_x_continuous(limits=c(0,100))+
scale_y_continuous(limits=c(-2,2),breaks=seq(-2,2,0.1),expand=c(0.01,0.01))+
theme_bw(base_size=12, base_family="Helvetica") +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
text = element_text(size=10),
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_rect(colour="black",fill=NA,size=0.5),
panel.background = element_blank(),
legend.position="top" ,
legend.direction="vertical",
legend.title=element_blank(),
legend.text=element_text(size=13),
legend.background=element_blank(),
legend.key=element_blank())+
labs(x='', y='',title="")+
theme(plot.title=element_text(size=10))+
theme(strip.text.x = element_text(size = 8,color="black"),strip.background=element_blank())+
theme(strip.text.x = element_text(size = 8,color="black"))+
scale_colour_discrete(name ="Factor")+
scale_linetype_discrete(name ="Factor") +
scale_shape_discrete(name ="Factor")

Resources