I'm a non-programmer who badly needs to plot a multivariate kernel density function with two variables against each other to better understand my data. After searching the internet I found a code which I used to insert my data and create the desired function.
This is my current code:
mydata = read.csv("J:/LKAB Thesis/Plots/Kernel/81241.txt", header = TRUE, sep = "\t")
x1 <- mydata[,11]
x2 <- mydata[,23]
df <- data.frame(x1,x2)
x <- densCols(x1,x2, colramp=colorRampPalette(c("black", "white")))
df$dens <- col2rgb(x)[1,] + 1L
cols <- colorRampPalette(c("#000099", "#00FEFF", "#45FE4F",
"#FCFF00", "#FF9400", "#FF3100"))(32)
df$col <- cols[df$dens]
plot(x2~x1, data=df[order(df$dens),], pch=20, col=col, cex=1)
And this is the resulting plot:
This is exactly what I need, but why is the dense part of the plot white? It should be filled with a nice smooth red color to indicate the high density. How do I fix this?? If I change the colorRampPalette
to a higher value like 256, the hole is filled but the rest of the plot is much uglier and less detailed which I don't want.
Please help, I need this to finish my thesis but bear in mind that I'm a beginner and don't really know anything about R. Unfortunately I need to use it for this plot!
Here is some sample data which can be used for this example. These are measured concentrations of two elements, Ti and Zr which I want to plot against each other and see the densities of the points.
d <- read.table("http://pastebin.com/raw.php?i=DcFdZ5Rm")
Thanks!
Two solutions here. In both cases, if you want to display the points (rather than the more standard density contours; see ggplot2 example), then it is important that you deal with overplotting by making your points transparent.
#Data import and cleaning
d <- read.table(
"http://pastebin.com/raw.php?i=DcFdZ5Rm",
header = TRUE,
stringsAsFactors = FALSE
)
d[] <- lapply(d, as.numeric)
# Draw the plot using ggplot. It's nicer.
# Here's a scatterplot with density contours.
library(ggplot2)
(p_contours <- ggplot(d, aes(Ti, Zr)) +
geom_point(alpha = 0.1) +
geom_density2d()
)
# Here's a heatmap. Explore different binwidth values.
(p_heatmap <- ggplot(d, aes(Ti, Zr)) +
geom_bin2d(binwidth = c(0.001, 0.001))
)
#If you insist on using base graphics, try something like
library(scales)
col <- densCols(d$Ti, d$Zr, colramp=colorRampPalette(c("grey67", "blue")))
col <- alpha(col, 0.1) # Make points transparent
plot(d$Ti, d$Zr, col = col, pch = 16)
why is the dense part of the plot white?
Because you chose a black and white colour ramp, with white representing the highest density region. Because you only specified colours for the first 32 elements in density, so some points have a missing colour value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.