Is it possible to extract specific sections of a ggplot figure/map and place them side by side in a secondary figure but still add points to the three frames as if they were still one plot i.e. for the following map
create a map split into 3 sections which can then be manipulated as one graph (i.e. adding points to all three sections of the graph simultaneously?
UPDATE: Reproducible example
set.seed(1)
dfx<-c(sample(1:1000,100),sample(2000:3000,100),sample(4000:3000,100))
dfy<-c(sample(1:1000,100),sample(2000:3000,100),sample(4000:3000,100))
p<-ggplot()+
coord_fixed()+
geom_point(aes(x=dfx,y=dfy))
p
I can get partway there but can't retain the effects of coord_equal or coord_fixed while allowing free scales ... hopefully someone else can step in and get the rest of the way. (This has come up before -- scatterplot with equal axes -- but I haven't seen a solution.)
dd <- data.frame(dfx,dfy)
dd2 <- transform(dd,panel=cut(dfx,seq(0,4000,by=1000),labels=1:4))
p <- ggplot(dd2)+geom_point(aes(dfx,dfy)) + coord_equal()
p + facet_wrap(~panel,nrow=1,scale="free")
Related
I am trying to put labels beside some points which are very close to each other on geographic coordinate. Of course, the problem is overlapping labels. I have used the following posts for reference:
geom_text() with overlapping labels
avoid overlapping labels in ggplot2 charts
Relative positioning of geom_text in ggplot2?
The problem is that I do not want to relocate labels but increase the interval of labeling (for example every other 10 points).
I tried to make column as alpha in my dataframe to make unwanted points transparent
[![combined_df_c$alpha=rep(c(1,rep(0,times=11)),
times=length(combined_df_c$time)/
length(rep(c(1,rep(0,times=11)))))][1]][1]
I do not know why it does not affect the plot and all labels are plotted again.
The expected output is fewer labels on my plot.
You can do this by sequencing your dataframe for the labs of geom_text.
I used the build-in dataset mtcars for this, since you did not provide any data. With df[seq(1,nrow(df),6),] i slice the data with 6-steps. This are the labels which get shown in your graph afterwards. You could use this with any steps you want. The sliced dataframe is given to geom_text, so it does not use the original dataset anymore, just the sliced one. This way the amount of points for the labels and the amount of labels are equal.
df <- mtcars
labdf<- df[seq(1,nrow(df),6),]
ggplot()+
geom_point(data=df, aes(x=drat, y=seq(1:length(drat))))+
geom_text(data=labdf,
aes(x=drat, y=seq(1:length(drat))), label=labdf$drat)
The output is as expected: from 32 rows, just 6 get labeled.
You can easily adjust the code for your case.
also: you can put the aes in ggplot() which may be more useful if you use more then just gemo_point. I made it like this, so i can clarify: there is a different dataset used on geom_text()
I'm trying plots a graph lines using ggplot library in R, but I get a good plots but I need reduce the gradual space or height between rows grid lines because I get big separation between lines.
This is my R script:
library(ggplot2)
library(reshape2)
data <- read.csv('/Users/keepo/Desktop/G.Con/Int18/input-int18.csv')
chart_data <- melt(data, id='NRO')
names(chart_data) <- c('NRO', 'leyenda', 'DTF')
ggplot() +
geom_line(data = chart_data, aes(x = NRO, y = DTF, color = leyenda), size = 1)+
xlab("iteraciones") +
ylab("valores")
and this is my actual graphs:
..the first line is very distant from the second. How I can reduce heigth?
regards.
The lines are far apart because the values of the variable plotted on the y-axis are far apart. If you need them closer together, you fundamentally have 3 options:
change the scale (e.g. convert the plot to a log scale), although this can make it harder for people to interpret the numbers. This can also change the behavior of each line, not just change the space between the lines. I'm guessing this isn't what you will want, ultimately.
normalize the data. If the actual value of the variable on the y-axis isn't important, just standardize the data (separately for each value of leyenda).
As stated above, you can graph each line separately. The main drawback here is that you need 3 graphs where 1 might do.
Not recommended:
I know that some graphs will have the a "squiggle" to change scales or skip space. Generally, this is considered poor practice (and I doubt it's an option in ggplot2 because it masks the true separation between the data points. If you really do want a gap, I would look at this post: axis.break and ggplot2 or gap.plot? plot may be too complexe
In a nutshell, the answer here depends on what your numbers mean. What is the story you are trying to tell? Is the important feature of your plots the change between them (in which case, normalizing might be your best option), or the actual numbers themselves (in which case, the space is relevant).
you could use an axis transformation that maps your data to the screen in a non-linear fashion,
fun_trans <- function(x){
d <- data.frame(x=c(800, 2500, 3100), y=c(800,1950, 3100))
model1 <- lm(y~poly(x,2), data=d)
model2 <- lm(x~poly(y,2), data=d)
scales::trans_new("fun",
function(x) as.vector(predict(model1,data.frame(x=x))),
function(x) as.vector(predict(model2,data.frame(y=x))))
}
last_plot() + scale_y_continuous(trans = "fun")
enter image description here
How to plot heatmap with multiple categories in a single cell with ggplot2? Heatmap plot of categorical variables could be done with this code
#data
datf <- data.frame(indv=factor(paste("ID", 1:20),
levels =rev(paste("ID", 1:20))), matrix(sample(LETTERS[1:7], 400, T), ncol = 20))
library(ggplot2);
library(reshape2)
# converting data to long form for ggplot2 use
datf1 <- melt(datf, id.var = 'indv')
ggplot(datf1, aes(variable, indv)) + geom_tile(aes(fill = value),
colour = "white") + scale_fill_manual(values= rainbow (7))
The codes came from here:
http://rgraphgallery.blogspot.com/2013/04/rg54-heatmap-plot-of-categorical.html
But what about multiple categories in a single cell like this? Is it possible to use triangle or other shape as a cell?
http://postimg.org/image/4dudrv0nz/
copy from biostar as Alex Reynolds suggested.
For those interested, this apperas to be Figure 2 from Exome sequencing identifies mutation in CNOT3 and ribosomal genes RPL5 and RPL10 in T-cell acute lymphoblastic leukemia.
I wanted to create a similar plot with ggplot and geom_tile for a bigger collection of genes (few hundreds) but finally decided to use geom_points instead to provide additional information per cell (tile). Also it looks to me a lot like this plot was generated in Excel or some other spreadsheet software (maybe along those lines https://www.youtube.com/watch?v=0s5OiRMMzuY). The colors in the cells (tiles) do not match those in the legend (suggesting that they have been added separately and not automatically) and there appears to be an erroneous cell (diagonal separating colors -upper left to lower right - different from diagonal in black color - lower left to upper right -).
Hence, my concluding two cents: Doing this automatically is probably very time-consuming and in my opinion makes only sense if you want to do this repeatedly, e.g., on data that is subject to change or on multiple datasets, and/or if you have a larger collections of genes.
Otherwise, following the instructions in the youtube video for a rather small number of cells is likely to be more efficient. Or use geom_point (similar to Adding points to a geom_tile layer in ggplot2 or
Marking specific tiles in geom_tile() / geom_raster()
) to represent information about an additional category (variable).
In any case, should anyone have other suggestions on how to automatically create such a figure, I am more than happy to hear about that.
I am looking to create a plot in R that shows the relative change of some variables between two factors. I would like to stack them to reduce redundant text and make it easy to visually compare the changes between the two factors. I would like it to look something like this: http://postimg.org/image/clmw5zj37/.
where the lines (or bars) represent the relative change (y) in each variable (X), a solid circle (or any other symbol) represents no change, and an asterisk indicates the the change is statistically significant. Anyone have an idea of how to accomplish this in R?
This?
set.seed(1)
df <- data.frame(x=toupper(letters[1:10]),
y=rnorm(20,0,50),
sig=sample(0:1,20,replace=T),
factor=rep(c("Factor1","Factor2"),each=10))
library(ggplot2)
ggplot(df) +
geom_point(aes(x=x,y=y),shape=1,size=3)+
geom_linerange(aes(x=x,ymin=0,ymax=y))+
geom_text(data=df[df$sig==1,], aes(x=x,y=y+10*sign(y)),label="*",size=10)+
geom_hline(yintercept=0)+
facet_grid(factor~.)
Note that it is considered polite to provide a representative dataset. See this link for the way to formulate a question well.
Edit In response to OP's comment.
To plot points only wheny=0, set data=df[df$y==0,] in the call to geom_point(...). Vertical alignment of the stars can be done using vjust= in the call to geom_text(...). So, this code:
set.seed(1)
df <- data.frame(x=toupper(letters[1:10]),
y=rnorm(20,0,50),
sig=sample(0:1,20,replace=T),
factor=rep(c("Factor1","Factor2"),each=10))
df[sample(1:nrow(df),4),2:3]=0 # add some zeros to example
library(ggplot2)
ggplot(df) +
geom_point(data=df[df$y==0,],aes(x=x,y=y),size=5)+
geom_linerange(aes(x=x,ymin=0,ymax=y))+
geom_text(data=df[df$sig==1,], aes(x=x,y=y+10*sign(y)),
label="*", size=10, vjust=+0.65)+
geom_hline(yintercept=0)+
facet_grid(factor~.)
Genreates this ggplot:
I have several data and I need to plot them compactly in a picture like this:
I already tried par() layout() and ggplot() but plots are displayed so far each other.
I need them to be very close, as if they were in the same plot with a different y (e.g. plot1 y=0, plot2 y=1, plot3 y=3 and so on..)
Can someone help me?
That can be acquired using the layout, also, but maybe an easier approach is to set the graphical parameters in a suitable way.
Function par() let's you specify the number of panels in a single figure using the argument mfrow. It takes a vector of two numbers, that specify the number sub-figure rows and columns. For example, c(2,1) would create two rows of figure,s but only a single column. That's what is in your example figure. You can change the number of figure rows to the number of sub-figures you would like to plot vertically.
In addition, the margins around each sub-figure can be set using the argument mar. The margins are specified in the order of 1. bottom, 2. left, 3. top., and 4. right. Making the bottom and top margins smaller would draw your sub-figures closer together.
In R this could look something like the following:
# Simulate some random data
a<-runif(10000)
b<-runif(10000)
# Open a new plot windows
# width: 7 inches, height: 2 inches
x11(width=7, height=1)
# Specify the number of sub-figures
# Specify the margins (top and bottom are 0.1, left and right are 2)
# Needs some experimenting with to get these right
par(mfrow=c(2,1), mar=c(0.1,2,0.1,2))
# Plot the figures
barplot(a)
barplot(b)
The resulting figure should roughly resemble this:
Here is ggplot version using facet_grid:
df <- data.frame(a=runif(3e3), b=rep(letters[1:3], 1e3), c=rep(1:1e3, 3))
ggplot(df, aes(y=a, x=c)) + geom_bar(stat="identity") + facet_grid(b ~ .)