My name is Venus , I am a beginner of data mining and use R. Right now, I need fill my datas to map and show it with different colours to see the different level.
I have install ggplot library and input my data with longtitue and latitude
My code like this
> ggplot(target_map, aes(long, lat, group = group, fill = Target.rate)) +
+ geom_polygon(colour = alpha("black", 1/2), size = 0.2) +
+ geom_polygon(data = needregion, colour ="white", fill = NA)+
+ scale_fill_brewer(palette = "PuRd")
I do this from an example, but I don't know why R always return this result:
Error: Aesthetics must be either length 1 or the same as the data (19584): x, y, group, fill
So, Could you give me some suggest what wrong with me? Thank you a lot
Related
I have two ggplots. The first 1 looks like this:
ggplot(nurse, aes(x = nurse$z2.bk, y = nurse$z1.bk, color = nurse$phoneme)) +
geom_point() +
scale_x_reverse() + scale_y_reverse() +
scale_color_discrete() +
theme_classic()
I then created a subset which calculates the z1.bk averages and z2.bk for each of the phoneme categories.
mean_F1 = the z1.bk average and mean_F2 = the z1.bk average.
vowel mean_F1 mean_F2
<fct> <dbl> <dbl>
1 Er 0.00830 0.612
2 Ir -0.0433 0.0456
3 Vr 0.0365 -0.576
I then created another ggplot (below) for these values and labelled them according to the nurse$phoneme values. I just renamed them here to vowels to keep everything a bit cleaner.
ggplot(means, aes(x = mean_F2, y = mean_F1, label = vowel)) +
geom_label() +
scale_x_reverse() + scale_y_reverse() +
theme_classic()
I now wanted to overlay them, so that the labels are displayed above the other points in the corresponding colour, i.e. Er in red.... I tried the following but got an error message.
ggplot(nurse, aes(x = nurse$z2.bk, y = nurse$z1.bk, color = nurse$phoneme, label = means$vowel)) +
geom_point() +
geom_label(data = means, aes(x = mean_F2, y = mean_F1)) +
scale_x_reverse() + scale_y_reverse() +
theme_classic()
Error: Aesthetics must be either length 1 or the same as the data (563): label
If I change 'label = means$vowel' to just 'vowel', I get another error message saying the object can't be found. If I change it to nurse$phoneme, I get this error message Error: Aesthetics must be either length 1 or the same as the data (3): colour, label.
How do I combine them properly? If I need to supply you with more data, just let me know. And thanks in advance!
First, it's a bit of bad form to use the $ convention to call columns in ggplot2, where you should simply give the name of the column in the dataset: thus nurse$z2.bk becomes simply z2.bk in the aes() call. With that being said, you can use it and it should still work... it's just frowned upon. :)
Now, for the error message you are receiving - this is because the aesthetic for label= is indicated in your ggplot() call to be means$vowel, but in the dataset nurse, there are 563 observations. Since you have two datasets being applied separately to your point and label geoms, I would state them within the aes() for each geom.
Without your full dataset, I can't confirm, but this should work below. Note also that I'm indicating a label for the legend for color, because it is likely that calling the two columns in the separate datasets with different names could split the legend. Setting the name of the legend to be the same (and having the same labels in each) should keep the two color legends together.
ggplot(nurse, aes(x = z2.bk, y = z1.bk, color = phoneme)) +
geom_point() +
geom_label(data = means, aes(x = mean_F2, y = mean_F1, label=vowel, color=vowel)) +
scale_x_reverse() + scale_y_reverse() +
labs(color='The colors') +
theme_classic()
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 am trying to create some maps in R utilising ggplot2. I've reached a point where I have the final map I want spatially, but need to change the shape and colour of the points according to categorial factors.
The data frame is called genAQ in this context. Each row contains longitude and latitude, quality (high/low) and species (3 sp). Eventually, I would like the point colour to be in accordance with species, and the point shape to be in accordance with quality.
#correct spatial map, but all points are black. This code works perfectly.
ggplot(data=world) +
geom_sf() +
geom_point(data=genAQ, aes(x=Lon, y=Lat), size=1,
shape = 19, col = "black") +
geom_polygon(data=lakes10, aes(long, lat, group = group), fill="lightblue") +
coord_sf(xlim = c(133.75, 140.52), ylim = c(-26.75, -31.30), expand=F)
Now I try to change the colour, utilising species as a factor.
If I were coding using the plot function, I would write it as
plot(genAQ$Lat~genAQ$Lon, pch=19, col=genAQ$Species).
Using this same principle, I try to apply it to the initial code which works.
ggplot(data=world) +
geom_sf() +
geom_point(data=genAQ, aes(x=Lon, y=Lat), size=1,
shape = 19, col = genAQ$Species) +
geom_polygon(data=lakes10, aes(long, lat, group = group), fill="lightblue") +
coord_sf(xlim = c(133.75, 140.52), ylim = c(-26.75, -31.30), expand=F)
It comes back with an error:
Error in grDevices::col2rgb(colour, TRUE) :
invalid color name 'Amytornis modestus indulkanna'.
So clearly ggplot does not understand the same language I'm using in the plot() function, as it's still trying to read the colour as a colour, rather than a categorical factor. I've tried other methods I've seen on StackOverflow, but I can't seem to find an answer for a seemly simple problem.
I'm trying to plot a scatter graph with error bars in R using categorical data on the x axis, using the following code:
Nesk <- read.table("E:\\R stuff\\Chapter 2\\Boxplots of nb\\NEnbNOINF.txt", header=TRUE, fill=TRUE)
pd <- position_dodge(0.2)
ggplot(Nesk, aes(x = TYPE, y = NB, color = TYPE)) +
geom_jitter() +
geom_point(position = pd) +
geom_errorbar(aes(ymin = LC, ymax = UC), position = pd) +
theme_bw() +
theme(axis.title = element_text(face = "bold")) +
ylab("Nb")
However, I can't get the error bars on the jittered points. I end up with this https://imgur.com/qBcvOat. Sorry all, don't have the reputation to directly insert images
I've tried using position dodge however I'm aware that it just separates the points by category (COL, LIN, NOM) as opposed to within each category. Is there any way I can jitter the points and attach error bars to these? I've seen some posts with fixes for this, but I think somewhere along the line an update invalidated those.
Thanks in advance!
I have got a map with a legend gradient and I would like to add a box for the NA values. My question is really similar to this one and this one. Also I have read this topic, but I can't find a "nice" solution somewhere or maybe there isn't any?
Here is an reproducible example:
library(ggplot2)
map <- map_data("world")
map$value <- setNames(sample(-50:50, length(unique(map$region)), TRUE),
unique(map$region))[map$region]
map[map$region == "Russia", "value"] <- NA
ggplot() +
geom_polygon(data = map,
aes(long, lat, group = group, fill = value)) +
scale_fill_gradient2(low = "brown3", mid = "cornsilk1", high = "turquoise4",
limits = c(-50, 50),
na.value = "black")
So I would like to add a black box for the NA value for Russia. I know, I can replace the NA's by a number, so it will appear in the gradient and I think, I can write a workaround like the following, but all this workarounds do not seem like a pretty solution for me and also I would like to avoid "senseless" warnings:
ggplot() +
geom_polygon(data = map,
aes(long, lat, group = group, fill = value)) +
scale_fill_gradient2(low = "brown3", mid = "cornsilk1", high = "turquoise4",
limits = c(-50, 50),
na.value = "black") +
geom_point(aes(x = -100, y = -50, size = "NA"), shape = NA, colour = "black") +
guides(size = guide_legend("NA", override.aes = list(shape = 15, size = 10)))
Warning messages:
1: Using size for a discrete variable is not advised.
2: Removed 1 rows containing missing values (geom_point).
One approach is to split your value variable into a discrete scale. I have done this using cut(). You can then use a discrete color scale where "NA" is one of the distinct colors labels. I have used scale_fill_brewer(), but there are other ways to do this.
map$discrete_value = cut(map$value, breaks=seq(from=-50, to=50, length.out=8))
p = ggplot() +
geom_polygon(data=map, aes(long, lat, group=group, fill=discrete_value)) +
scale_fill_brewer(palette="RdYlBu", na.value="black") +
coord_quickmap()
ggsave("map.png", plot=p, width=10, height=5, dpi=150)
Another solution
Because the original poster said they need to retain the color gradient scale and the colorbar-style legend, I am posting another possible solution. It has 3 components:
We need to trick ggplot into drawing a separate color scale by using aes() to map something to color. I mapped a column of empty strings using aes(colour="").
To ensure that we do not draw a colored boundary around each polygon, I specified a manual color scale with a single possible value, NA.
Finally, guides() along with override.aes is used to ensure the new color legend is drawn as the correct color.
p2 = ggplot() +
geom_polygon(data=map, aes(long, lat, group=group, fill=value, colour="")) +
scale_fill_gradient2(low="brown3", mid="cornsilk1", high="turquoise4",
limits=c(-50, 50), na.value="black") +
scale_colour_manual(values=NA) +
guides(colour=guide_legend("No data", override.aes=list(colour="black")))
ggsave("map2.png", plot=p2, width=10, height=5, dpi=150)
It's possible, but I did it years ago. You can't use guides. You have to set individually the continuous scale for the values as well as the discrete scale for the NAs. This is what the error is telling you and this is how ggplot2 works. Did you try using both scale_continuous and scale_discrete since your set up is rather awkward, instead of simply using guides which is basically used for simple plot designs?