I am attempting to plot a small raster using ggplot(gplot) with a manual color scale for the seven discrete values in the raster. However, scale_colour_manual does not seem to be working as expected (and as indicated in other similar posts on this and other sites) and instead resorts to the default coloration.
Here is the incorrect ggplot version:
Here is the correct coloration using R plot:
Here is my sample raster:
https://dl.dropboxusercontent.com/u/11618932/map.tif
or
https://dl.dropboxusercontent.com/u/11618932/map.r
And here is the code:
library(raster)
library(ggplot2)
my_raster<-raster("map.tif")
my_color<-c("#177798","#D239A0","#A3E0AE","#D29D52","#8F4F3E","#917FD9","#6EC848")
gplot(my_raster) + geom_tile(aes(fill = as.factor(value))) + scale_colour_manual(values= c(my_color))
Thanks in advance for your help
Related
I found this on the Tidyverse Github:
https://github.com/tidyverse/ggplot2/issues/3716
but I can't find the resolution of yutannihilation's question.
For exploratory data analysis, I would like for the outline stroke to reach the x-axis as it does with base R, including facets with scales="free".
Is there a way to do this programmatically? The user may have multiple facets of data, on the same or different scales. Can I ensure the x-axis is wide enough to take the density to zero?
I have tried outline.type = "full" and "both" but neither seem to work.
The MRE shows the issue. The use case is within a Shiny app and can be facet_wrap-ed as well.
Thanks!
#R base
plot(density(diamonds$carat, adjust = 5))
#ggplot
library(ggplot2)
ggplot(diamonds, aes(carat)) +
geom_density(adjust = 5)
A straightforward solution would be to calculate the density yourself and plot that:
library(ggplot2)
ggplot(as.data.frame(density(diamonds$carat, adjust = 5)[1:2]), aes(x, y)) +
geom_line()
I want to make an area plot with ggplot(mpg, aes(x=year,y=hwy, fill=manufacturer)) + geom_area(), but I get this:
I'm realy new in R world, can anyone explain why it does not fill the area between the lines? Thanks!
First of all, there's nothing wrong with your code. It's working as intended and you are correct in the syntax required to do what you are looking to do.
Why don't you get the area geom to plot correctly, then? Simple answer is that you don't have enough points to draw a proper line between your x values for all of the aesthetics (manufacturers). Try the geom_point plot and you'll see what I mean:
ggplot(mpg, aes(x=year,y=hwy)) + geom_point(aes(color=manufacturer))
You need a different dataset. Here's a dummy one that is simply two lines with different slopes. It works as expected because each of the aesthetics has y values which span the x labels:
# dummy dataset
df <- data.frame(
x=rep(1:10,2),
y=c(seq(1,10,length.out=10), seq(1,5,length.out=10)),
z=c(rep('A',10), rep('B', 10))
)
# plot
ggplot(df, aes(x,y)) + geom_area(aes(fill=z))
I am doing kmeans clustering on a png image and have been plotting it using grid::grid.raster(image). But I would like to put a legend which shows the intensity in a bar(from blue to red) marked with values, essentially indicating the intensity on the image. (image is an array where the third dimension equals 3 giving the red, green and blue channels.)
I thought of using grid.legend() but couldn't figure it out. I am hoping the community can help me out. Following is the image I have been using and after I perform kmeans clustering want a legend beside it that displays intensity on a continuous scale on a color bar.
Also I tried with ggplot2 and could plot the image but still couldn't plot the legend. I am providing the ggplot code for plotting the image. I can extract the RGB channels separately using ggplot2 also, so showing that also helps.
colassign <- rgb(Kmeans2#centers[clusters(Kmeans2),])
library(ggplot2)
ggplot(data = imgVEC, aes(x = x, y = y)) +
geom_point(colour = colassign) +
labs(title = paste("k-Means Clustering of", kClusters, "Colours")) +
xlab("x") +
ylab("y")
Did not find a way to use grid.raster() properly but found a way to do it by ggplot2 when plotting the RGB channels separately. Note: this only works for plotting the pannels separately, but this is what I needed. Following shows the code for green channel.
#RGB channels are respectively stored in columns 1,2,3.
#x-axis and y-axis values are stored in columns 4,5.
#original image is a nx5 matrix
ggplot(original_img[,c(3,4,5)], aes(x, y)) +
geom_point(aes(colour = segmented_img[,3])) +
scale_color_gradient2()+
# scale_color_distiller(palette="RdYlBu") can be used instead of scale_color_gradient2() to get color selections of choice using palette as argument.
I have some very dense data, so I would like to use geom_hex to visualize the distribution. However, they are very concentrated, such that it would dramatically help if I could jitter the underlying points.
The following works fine, but has no jitter:
ggplot(data=data, aes(x=x,y=y))+
geom_hex()
I tried the following:
ggplot(data=data, aes(x=x,y=y))+
geom_hex(position=position_jitter(width = 0.1, height = 0.1))
However, this produced an empty graph (labels, but not content).
Any suggestions for how to apply jitter within geom_hex is much appreciated.
I want to create a scatter plot, in which each point is a tiny pie chart. For instance consider following data:
foo <- data.frame(X=runif(30), Y=runif(30),A=runif(30),B=runif(30),C=runif(30))
The following code will make a scatter plot, representing X and Y values of each point:
library(reshape2)
library(ggplot2)
foo.m <- melt(foo, id.vars=c("X","Y"))
ggplot(foo.m, aes(X,Y))+geom_point()
And the following code will make a pie chart for each point:
p <- ggplot(foo.m, aes(variable,value,fill=variable)) + geom_bar(stat="identity")
p + coord_polar() + facet_wrap(~X+Y,,ncol=6) + theme_bw()
But I am looking to merge them: creating a scatter plot in which each point is replaced by the pie chart. This way I will be able to show all 5 values (X, Y, A, B, C) of each record in the same chart.
Is there anyway to do it?
This is the sort of thing you can do with package ggsubplot. Unfortunately, according to issue #10 here, this package is not working with R 3.1.1. I ran it successfully if I used an older version of R (3.0.3).
Using your long dataset, you could put bar plots at each X, Y point like this:
library(ggplot2)
library(ggsubplot)
ggplot(foo.m) +
geom_subplot2d(aes(x = X, y = Y,
subplot = geom_bar(aes(variable, value, fill = variable), stat = "identity")),
width = rel(.5), ref = NULL)
This gives the basic idea, although there are many other options (like controlling where the subplots move to when there is overlap in plot space).
This answer has more information on the status of ggsubplot with newer R versions.
there is a package, scatterpie, that does exactly what you want to do!
library(ggplot2)
library(scatterpie)
ggplot() +
geom_scatterpie(aes(x=X, y=Y, r=0.1), data=foo.m, cols=c("A", "B", "C"))
In the aesthetics, r is the radius of the pie, you can adjust as necessary. It is dependent on the scale of the graph - since your graph goes from 0.0 to 1.0, a radius of 1 would take up the entire graph (if centered at 0.5, 0.5).
Do note that while you will get a legend for the pie slice colors, it will not (to my knowledge) label the slices themselves on the pies.