I've created a heatmap using ggplot
library(plyr)
library(scales)
guide_ind <- ddply(guide_tag[company == FALSE], .(tag), transform, rescale = rescale(count))
(p <- ggplotly(ggplot(guide_ind, aes(tag, username)) +
geom_tile(aes(fill = rescale),colour = "white") +
scale_fill_gradient(low = "white",high = "steelblue") +
theme(axis.text.x = element_text(angle= 90, hjust=1), legend.position= "bottom") )
)
I have about 700 rows of user name, and I would like to make sure that all the usernames are visible in document so that when I produce this in markdown, it will show the names individually instead of overlapping like the picture below.
I've tried using the fig.height, and gplot heatmap, but neither has worked.
Does anyone have suggestions to how to make all data points visible on the yaxis?
Related
I am trying to visualize gene ontology data from David. I was able to create a bar plot and have arranged my data in descending order. I would like to organize this further based on its Category/fill color (to maintain same descending order per category). Any pointers will be helpful. PFA my R script below and the image that I have so far.
library(readr)
#import read CSV file
HDvC <- read_csv("G0_all_HDvscontrol.csv")
head(HDvC)
library(ggplot2)
myplot1 <- ggplot(HDvC, aes(x = reorder(Description, neglogFDR),
y = neglogFDR, fill = Category)) +
geom_bar(stat = "identity", position = "dodge") +
coord_flip() + labs(y = "-log (q value)",
x = "GO Description",
title = "HD vs Control") + theme_gray()
theme(plot.title = element_text(
hjust = 0.5, # center
size = 12,
color = "gray0",
face = "bold" )
)
myplot1
Can you try facet?
Add to your ggplot code:
+ facet_wrap(.~Category)
I've made a histogram graph that shows the distribution of lidar returns per elevation for three lidar scans I have done.
I've converted my data to long format, with:
one column called 'value', describing the z position of each point
one column called 'variable', containing the name of each
scan group
In the attached image you can see the histograms of my three scan groups. I am currently using viridis to color the histogram by scan group (ie. the name of the scan in the variable column). However, I want to match the colours in the graph with colours I already have.
How might I do this?
The hexcols I'd like to like color each of my three histograms with are:
lightgreen = "#62FE96"
lightred = "#FE206B"
darkpurple = "#62278E"
A link to my data - 'density2'
My current code:
library(tidyverse)
library(viridisLite)
library(viridis)
# histogram
p <- density2 %>%
ggplot( aes(x=value,color = variable, show.legend = FALSE)) +
geom_histogram(binwidth = 1, alpha = 0.5, position="identity") +
scale_color_viridis(discrete =TRUE) +
scale_fill_viridis(discrete=TRUE) +
theme_bw() +
labs(fill="") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
p + scale_y_sqrt() + theme(legend.position="none") + labs(y = "data pts", x = "elevation (m)")
Any help would be most appreciated!
Delete the scale_color_viridis and scale_fill_viridis lines - these are applying the Viridis color scale. Replace with scale_fill_manual(values = c(lightgreen, lightred, darkpurple)). And in your aesthetic mapping replace color = variable with fill = variable. For a histogram, color refers to the color of the lines outlining each bar, and fill refers to the color each bar is filled in.
This should leave you with:
p <- density2 %>%
ggplot(aes(x = value, fill = variable)) +
geom_histogram(binwidth = 1, alpha = 0.5, position = "identity") +
scale_fill_manual(values = c(lightgreen, lightred, darkpurple)) +
theme_bw() +
labs(fill = "") +
theme(panel.grid = element_blank())
p + scale_y_sqrt() +
theme(legend.position = "none") +
labs(y = "data pts", x = "elevation (m)")
I've also done some other clean-up. show.legend = FALSE does not belong inside aes() - and your theme(legend.position = "none") should take care of it.
I did not download your data, save it in my working directory, import it into R, and test this code on it. If you need more help, please post a small subset of your data in a copy/pasteable format (e.g., dput(density2[1:20, ]) for the first 20 rows---choose a suitable subset) and I'll be happy to test and adjust.
I have some data here
I read the data into a data frame and then plot this data with this following code,
# Reading data from a .csv file into a data frame
df <- read.table("newcsv_file.csv",header=T,sep="\t" )
# Now melting the data frame prior to plotting
df_mlt <- melt(df, id=names(df)[1], measure=names(df)[c(2, 6, 11,16,21,26,31,36,41,46,51,106,111,116,121,126,131,136,141,146,151)], variable = "cols")
# plotting the data
plt_fit <- ggplot(df_mlt, aes(x=x,y= value, color=cols)) +
geom_point(size=2) +
geom_smooth(method = "lm", se = FALSE) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) +
annotation_logticks(sides = "rl") +
theme_bw() +
theme(legend.text=element_text(size=12), legend.title=element_text(size=12))+
theme(axis.text=element_text(size=14)) +
theme(axis.title=element_text(size=14,face="bold")) +
labs(x = "x", y = "y") +
scale_color_discrete(name = "values", labels = c("0","-0.1","-0.2","-0.3","-0.4","-0.5","-0.6","-0.7","-0.8","-0.9","-1","+0.1","+0.2","+0.3","+0.4","+0.5","+0.6","+0.7","+0.8","+0.9","+1")) +
guides(colour = guide_legend(override.aes = list(size=3),nrow=2,title.position = 'top',title.hjust=0.5,legend.direction = "horizontal")) +
theme(legend.position = 'bottom', legend.margin=unit(1,"cm"),legend.background = element_rect(fill ='gray94')) +
theme(plot.margin=unit(c(0,2,0,0),"mm"))
The resulting plot looks like this, the problem here is that the right most edge of the legend is cropped.
I use +theme(legend.margin=unit(1,"cm")) but this does not seem sufficient. Could someone please let me know what I can change to display the full legend properly in the plot.
Thanks.
The code is fine. The problem is the size of your plot window. Try making it wider and you'll see the whole legend.
Also,
ggsave("plot_fit.pdf",plot_fit)
will create a pdf where the full legend is displayed.
After changing the width and height of the plot using the following code,
ggsave(file="new_png_file.png",width=22,height=21,units=c("cm"), dpi=600)
Yields a plot such as this,
I am looking for a type of plot that is essentially a grid. For example, there will be 10 columns and 50 rows. For example, something like this:
Each of the boxes (in this case, 10*50 = 500) will have a unique value that I will be providing via a data frame. Based on the unique values, I'll have a function that will assign a colour to each box. So then it becomes a grid to visualize "the range" of each box. I'd also need to label each of the columns (probably vertically so all labels fit) and rows (horizontally).
I just don't know what kind of plot that will be and I don't know if any libraries do this. I'm just looking for some help in finding something that does this. I'd appreciate some help if possible.
How about heatmap?
m=matrix(runif(12),3,4)
rownames(m)=c("Me","You","Him")
colnames(m)=c("We","Us","Them","I")
heatmap(m,NA,NA)
Note that it works on a matrix and not a data frame because all the values have to be numbers, and data frames are row-oriented records.
See the help for other options.
Look at the image function in the graphics package, or the rasterImage function if you want more control.
You could also build the plot up from scratch using the rect function.
I would go to ggplot2 for this as it allows a high degree of flexibility. In particular geom_tile is useful. If you actually want the panel lines you can comment out the theme(panel.grid.major = element_blank()) + and theme(panel.grid.minor = element_blank()) + lines and of course you can specify the colours as well. The text in each cell is optional; comment out the geom_text call if you don't need that. Note that you can control the size of the plot (rows and columns) simply by resizing the plot window or - if you want to output to a file using png() - by specifying the width and height arguments.
library(ggplot2)
library(reshape)
library(scales)
set.seed(1234)
num.els <- 5
mydf <- data.frame(category1 = rep(LETTERS[1:num.els], 1, each = num.els),
category2 = rep(1:num.els, num.els),
value = runif(num.els^2, 0, 100))
p <- ggplot(mydf, aes(x = category1,
y = category2,
fill = value)) +
geom_tile() +
geom_text(label = round(mydf$value, 2), size = 4, colour = "black") +
scale_fill_gradient2(low = "blue", high = "red",
limits = c(min(mydf$value), max(mydf$value)),
midpoint = median(mydf$value)) +
scale_x_discrete(expand = c(0,0)) +
scale_y_reverse() +
theme(panel.grid.minor = element_blank()) +
theme(panel.grid.major = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(panel.background = element_rect(fill = "transparent"))+
theme(legend.position = "none") +
theme()
print(p)
Output:
And resized:
Lets say you have a dataframe with "x" and "y" coordinates per each cell of the grid, and a variable "z" for each cell, and you loaded this dataframe in R called "intlgrid":
head(intlgrid)
x y z
243.742 6783.367 0.0035285
244.242 6783.367 0.0037111
244.742 6783.367 0.0039073
"..."
"so on..."
With ggplot2 package you can easily plot your raster. So:
install.packages("ggplot2")
once installed ggplot2, you just call it
library(ggplot2)
Now the code:
ggplot(intlgrid, aes(x,y, fill = z)) + geom_raster() + coord_equal()
And then you get your grid plotted.
I'm new to R and am trying to visualise data that is broken down by company and by year for a project at university. I want to try and add bars (as in bar chart bars) to the top and right hand side of the heatmap I've created below, to provide a direct method of comparing count data between the companies (right y axis), and between years for the group as a whole (top x axis).
I know I could probably merge images in Illustrator or something pre-publication, but I know the software is capable of adding together graphics (in a Lattice i think?) and I would like to improve my skills with R and ggplot.
Ideally I would also like to learn to:
1) add the values of each tile superimposed on top
geom_text(aes(fill = trialx.m$value, label = trialx.m$value)
doesn't seem to work for me?
2) Move the legend so that it was out of the way of the bar plots
3) Adjust the legend size and scale (to account for the data scaling)
4) order the heatmap vertically with the bar sizes
I know this is a lot, but I would appreciate help or advice about any part.
What I'm currently doing:
Re-scaling
library(ggplot2)
trialx.m <- melt(trialx)
trialx.m <- ddply(trialx.m, .(variable), transform, rescale = scale(value))
Plotting
(p <- ggplot(trialx.m, aes(variable, Company)) +
geom_tile(aes(fill = rescale), colour = "white") +
scale_fill_gradient(low = "ghostwhite", high = "darkblue"))
Neaten, remove background, rotate text etc.
p + theme_grey(base_size = base_size) + labs(x = "", y = "") +
scale_x_discrete(expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0)) +
opts(legend.position = "", axis.ticks = theme_blank(),
axis.text.x = theme_text(size = base_size * 0.8, angle = 90, hjust = 0,
colour = "grey50"))
Here is a dropbox link to the data I am using: