I need to overlay the same 'target_gene' plot on all facets. I can overlay it easily if I plot everything in 1 panel. However, when I combine this action with the facet_wrap, the 'target_gene' is plotted in a separate facet, rather than being overlaid on other plots. Please see the script below. The 'select_genes_plot' works (see same panel plots) but the 'select_genes_plot_facet' doesn't work (see plot in facets).
Please not that I cannot use +annotate(geom='point',x=target_gene$gene_exp.Time,y=target_gene$gene_exp.Value) because I also want to plot the sd error bars.
The transcriptome_reshape and target_gene data look like this, please see transcriptome data example, and target gene data)
library(ggplot2)
library(dplyr)
library(reshape2)
##1. Read transcriptome dataset
setwd('./')
transcriptome_data <- read.csv ('./Kaladp_expression_FPKM_average_sd.csv', header = TRUE)
reshape_data <- melt(transcriptome_data) #the function melt reshapes it from wide to long
average_geneexpression <- reshape_data[1:(743136/2),]
sd_geneexpression <- reshape_data[((743136/2)+1):743136,]
transcriptome_reshape <- cbind(average_geneexpression, sd_geneexpression)
colnames(transcriptome_reshape)[2]<- 'gene_exp.Time'
colnames(transcriptome_reshape)[3]<- 'gene_exp.Value'
colnames(transcriptome_reshape)[4]<- 'transcript_sd_data'
colnames(transcriptome_reshape)[5]<- 'sd.Time'
colnames(transcriptome_reshape)[6]<- 'sd.Value'
##2. Select ONE key target gene
target_gene <- transcriptome_reshape %>%
filter (Transcript %in% c('Kaladp0977s0008.1'))
##3.Plot options
##plot whole genome
ggplot(data = transcriptome_reshape, aes(x = gene_exp.Time, y = gene_exp.Value, group = Transcript)) +
geom_line()
##plot target gene alone
ggplot(target_gene, aes(x = gene_exp.Time, y = gene_exp.Value, group = 1)) +
geom_line()+
geom_point()
##plot all genes on same panel
select_genes_plot <- transcriptome_reshape %>%
filter (Transcript %in% c('Kaladp0498s0001.1','Kaladp0011s0858.1','Kaladp0071s0450.1')) %>%
ggplot(mapping = aes(x = gene_exp.Time, y = gene_exp.Value, color = Transcript)) +
geom_line(aes(group=Transcript))+
geom_point()+
geom_errorbar(aes(ymin = gene_exp.Value-sd.Value, ymax = gene_exp.Value+sd.Value), width = 0.2, position = position_dodge(0.0))+
geom_point(data = target_gene, aes(x = gene_exp.Time, y = gene_exp.Value), color = 'black', shape = 17)+ #this line plots target gene
geom_line(data = target_gene, aes(x = gene_exp.Time, y = gene_exp.Value, group=Transcript), color = 'black')+ #this line plots target gene
geom_errorbar(data = target_gene, aes(ymin = gene_exp.Value-sd.Value, ymax = gene_exp.Value+sd.Value),color = 'black', width = 0.2, position = position_dodge(0.0))#this line plots target gene
select_genes_plot
##plot in facets
select_genes_plot_facets <- transcriptome_reshape %>%
filter (Transcript %in% c('Kaladp0498s0001.1','Kaladp0011s0858.1','Kaladp0071s0450.1')) %>%
ggplot(mapping = aes(x = gene_exp.Time, y = gene_exp.Value, color = Transcript)) +
geom_line(aes(group=Transcript))+
geom_point()+
geom_errorbar(aes(ymin = gene_exp.Value-sd.Value, ymax = gene_exp.Value+sd.Value), width = 0.2, position = position_dodge(0.0))+
facet_wrap(facets = vars(Transcript), ncol = 3)+
geom_point(data = target_gene, aes(x = gene_exp.Time, y = gene_exp.Value), color = 'black', shape = 17)+ #this line plots target gene
geom_line(data = target_gene, aes(x = gene_exp.Time, y = gene_exp.Value, group = 1), color = 'black')+ #this line plots target gene
select_genes_plot_facets
Related
consider the following tibble
library(tidyverse)
df <-tibble(year = rep(1981:2020,4),
x = rep(letters[1:8],20),
y = rnorm(n = 160,0,1),
group = rep(letters[10:13],40))
I want to plot a faceted grid based on variable group and as text in each panel, the years (year) corresponding to each group (group).
Below a failed attempt where years are overlapping and not correct
ggplot() +
geom_line(data = df, aes(x = x, y = y, color = group)) +
geom_text(
data = df,
aes(
x = x,
y = 3,
label = year,
color = group
),
alpha = 0.7,
show.legend = FALSE
) +
facet_grid( ~ group)
Thanks for support!
I'm not sure I understand what you want, but you can try the following
ggplot() +
geom_line(data = df, aes(x = x, y = y, color = group)) +
geom_text(
data = df,
aes(
x = x,
y = 3,
label = year,
color = group
),
alpha = 0.7,
show.legend = FALSE,
position = position_stack(vjust = 1.5),
# stat = "unique"
) +
facet_grid( ~ group)
If you don't want the year to be repeated, uncomment the stat = "unique" line.
UPDATE
If you want a horizontal alignment you can create a new data frame
df2 <- df %>%
group_by(x, group) %>%
summarise(year = str_c(unique(year), collapse=", "))
ggplot() +
geom_line(data = df, aes(x = x, y = y, color = group)) +
geom_text(
data = df2,
aes(
x = x,
y = 1,
label = year,
color = group
),
alpha = 0.7,
show.legend = FALSE,
position = position_stack(vjust = 1.5),
stat = "unique"
) +
facet_grid( ~ group)
but with this alignment labels will overlap. You can try reducing the font-size or using facet_wrap to arrange the panels on two rows.
You can also manipulate strings in df2 and add "\n" where you need it, but I think this cannot be easily adapted to every string.
I want to create a black and white plot using ggplot2, where the data is plotted by category using a combination of lines and points. However, the legend only shows the point shape, with no line running through it, unless I add color to the plot.
Here is some example data to illustrate the problem with:
## Create example data
set.seed(123)
dat <- data.frame(
time_period = rep(1:4, each = 3),
category = rep(LETTERS[1:3], 4),
y = rnorm(12)
)
Here is an example of a color plot, so you can see how I want the legend to look:
library(ggplot2)
## Generate plot with color
ggplot(data = dat, mapping = aes(x = time_period, y = y, color = category)) +
geom_line(aes(group = category)) +
geom_point(aes(shape = category), size = 2) +
theme_bw()
However, if I move to grayscale (which I need to be able to do), the line running through the point in the legend disappears, which I'd like to avoid:
## Generate plot without color
ggplot(data = dat, mapping = aes(x = time_period, y = y)) +
geom_line(aes(group = category)) +
geom_point(aes(shape = category), size = 2) +
theme_bw()
How can I add a line through the point symbols in the legend with a grayscale plot?
I would suggest this approach:
#Plot
ggplot(data = dat, mapping = aes(x = time_period, y = y,group = category,shape = category)) +
geom_line(color='gray',show.legend = T) +
geom_point(size = 2) +
theme_bw()
Output:
I have the dataframe below:
etf_id<-c("a","b","c","d","e","a","b","c","d","e","a","b","c","d","e")
factor<-c("A","A","A","A","A","B","B","B","B","B","C","C","C","C","C")
normalized<-c(-0.048436801,2.850578601,1.551666490,0.928625186,-0.638111793,
-0.540615895,-0.501691539,-1.099239823,-0.040736139,-0.192048665,
0.198915407,-0.092525810,0.214317734,0.550478998,0.024613778)
df<-data.frame(etf_id,factor,normalized)
and I create a ggplotly() boxplot with:
library(ggplot2)
library(plotly)
ggplotly(ggplot(data = df, aes(x = factor, y = normalized)) +
geom_boxplot(aes(fill = as.factor(factor)),outlier.colour = 'black') +
geom_point(data = df, position = position_dodge(0.75))+geom_point(data = df,
aes(x = factor, y = normalized, shape = etf_id, color = etf_id),
size = 2))
I take as a result a boxplot with this legend:
but I want my legend to have only the color distinction like below. Note that the factors wont be 3 every time but may vary from 1 to 8.
The recommended way to alter plotly elements is to use the style() function. You can identify the elements and traces by inspecting plotly_json().
I'm not sure if there's a more compact way, but you can achieve the desired result using:
p <- ggplotly(ggplot(data = df, aes(x = factor, y = normalized)) +
geom_boxplot(aes(fill = as.factor(factor)),outlier.colour = 'black') +
geom_point(data = df, position = position_dodge(0.75))+geom_point(data = df,
aes(x = factor, y = normalized, shape = etf_id, color = etf_id),
size = 2))
p <- style(p, showlegend = FALSE, traces = 5:9)
for (i in seq_along(levels(df$factor))) {
p <- style(p, name = levels(df$factor)[i], traces = i)
}
p
Note that in this case the factor levels and traces align but that won't always be the case so you may need to adjust this (i.e. i + x).
One quick way would be to add show.legend = FALSE to supress the legend from showing.
library(ggplot2)
ggplot(data = df, aes(x = factor, y = normalized)) +
geom_boxplot(aes(fill = as.factor(factor)),outlier.colour = 'black') +
geom_point(position = position_dodge(0.75)) +
geom_point(aes(x = factor, y = normalized, shape = etf_id, color = etf_id),
size = 2, show.legend=FALSE)
Unfortunately, this does not work when this is passed to ggplotly. You can use theme(legend.position='none') which works but suppresses all the legends instead of specific ones. One dirty hack is to disable specific legend manually
temp_plot <- ggplotly(ggplot(data = df, aes(x = factor, y = normalized)) +
geom_boxplot(aes(fill = as.factor(factor)),outlier.colour = 'black') +
geom_point(position = position_dodge(0.75)) +
geom_point(aes(x = factor, y = normalized, shape = etf_id, color = etf_id),size = 2))
temp_plot[[1]][[1]][4:9] <- lapply(temp_plot[[1]][[1]][4:9], function(x) {x$showlegend <- FALSE;x})
temp_plot
I am plotting 2 sets of data on the same plot using ggplot. I have specified the colour for each data set, but there is no legend that comes out when the dot plot is generated.
What can i do to manually add a legend?
# Create an index to hold values of m from 1 to 100
m_index <- (1:100)
data_frame_50 <- data(prob_max_abs_cor_50)
data_frame_20 <- data.frame(prob_max_abs_cor_20)
library(ggplot2)
plot1 <- ggplot(data_frame_50, mapping = aes(x = m_index,
y = prob_max_abs_cor_50),
colour = 'red') +
geom_point() +
ggplot(data_frame_20, mapping = aes(x = m_index,
y = prob_max_abs_cor_20),
colour = 'blue') +
geom_point()
plot1 + labs(x = " Values of m ",
y = " Maximum Absolute Correlation ",
title = "Dot plot of probability")
First, I would suggest neatening your ggplot code a little. This is equivalent to your posted code;
ggplot() +
geom_point(data = data_frame_50, aes(x = m_index, y = prob_max_abs_cor_50,
colour = 'red')) +
geom_point(data = data_frame_20, aes(x = m_index, y = prob_max_abs_cor_20,
colour = 'blue')) +
labs(x = " Values of m ", y = " Maximum Absolute Correlation ",
title = "Dot plot of probability")
You won't get a legend here, because you are plotting different datasets with only one category in each. You need to have a single dataset with a column grouping your data (i.e. 20 or 50). So using some example data, this is the equivalent of what you are plotting and ggplot won't provide a legend;
ggplot() +
geom_point(data = iris, aes(x = Sepal.Length, y = Petal.Width), colour = 'red') +
geom_point(data = iris, aes(x = Sepal.Length, y = Petal.Length), colour = 'blue')
If you want to colour by category, include a colour argument inside the aes call;
ggplot() +
geom_point(data = iris, aes(x = Sepal.Length, y = Petal.Width,
colour = factor(Species)))
Have a look at the iris dataset to get a sense of how you need to shape your data. It's hard to give precise advice, because you haven't provided an idea of what your data look like, but something like this might work;
df.20 <- data.frame("m" = 1:100, "Group" = 20, "Numbers" = prob_max_abs_cor_20)
df.50 <- data.frame("m" = 1:100, "Group" = 50, "Numbers" = prob_max_abs_cor_50)
df.All <- rbind(df.20, df.50)
As the code below, I constructed a plotly from a ggplot, and want to change the position of the legend.
df <- data.frame(n=1:10, Var1 = rnorm(10), Var2 = rnorm(10))
data_long <- melt(df, id = "n")
a <- ggplot(data_long,
aes(x = n, y = value, colour = variable, group = variable)) +
geom_line()
ggplotly(a) %>% layout(legend = list(x = 0, y = 0), yaxis = list(title = ""))
The ggplot looks like
But the plotly looks like
Part of the legend title is still on the top of the graph.
You can use legend.position argument:
a <- ggplot(data_long,
aes(x = n, y = value, colour = variable, group = variable)) +
geom_line() +
theme(legend.justification=c(0,0), legend.position=c(0,0))