Error- State_count() must not be used ,. How to solve this? - r

She wants to find out crimes in the USA for various states. So she plots the data set "USArrests" to construct a bar chart of the number of murders in different states.
Following command is giving error :
ggplot(USArrests, aes(x = row.names(USArrests), y = USArrests$Murder, lab)) + geom_bar() + theme(axis.text.x=element_text(angle=90, hjust=1))
Error :
"stat_count() must not be used with a y aesthetic". What can she do to correct this?
Should I remove row.names — it should be x = USArrests$State ??
Should I use theme(axis.angle.x = 90) instead of current one?
Will geom_histogram() be better than Geom_bar?
Or Should I use geom_col() instead of geom_bar()?
Please do suggest correct command. Thanks

This is a very common issue, please see this. Just use geom_col instead of geom_bar, and also add row.names as a new variable to avoid further problems:
USArrests$states <- row.names(USArrests)
ggplot(USArrests, aes(x = states, y = Murder)) +
geom_col() +
theme(axis.text.x=element_text(angle=90, hjust=1))

Related

R; ggplot2: Overlaying 1 plot with another

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()

How to visualize several parameters in a barplot

I am trying to make a graph, were I have amount up the y-axis (numeric), Office place (categorical) on the x= axis, sorted in regions (categorical)...
What I have tried to do:
My_df %>%
filter(Context == "Humanitarian") %>%
ggplot(aes(Office_abb, Award_USD)) +
geom_histogram(aes(color=Region, fill = Region)) +
theme(legend.position="bottom") +
ggtitle("Overview of office amount pr. region")+
theme(axis.text.x = element_text(angle = 90, hjust = 1))
My error message on the above script is:
Error: stat_bin() must not be used with a y aesthetic.
I wold like it to be in a stacked diagram, or dodged - but when I tried to add
geom_bar(position = "dodge2")
into the equation, it didn't work either
It did however work with geom_point - but that is not the type of visualization that I wish for.
With that i am kind of lost on what to do - I hope that someone can help me move on from here! :-)

Y scale on ggplot2 bar graph makes no sense

My bar graph has a weird Y Axis that skips around seemingly at random, from -1.7% to -10.1%, -10.3%, and then to -2%. You can see it below:
Here is my code:
library(ggplot2)
healthd = read.csv("R/states.csv")
states = healthd[[1]]
insuredChange = healthd[[4]]
ggplot(data = healthd, aes(x = states, y = insuredChange)) +
geom_bar(stat="identity") +
theme(axis.text.x=element_text(angle = 90, hjust = 1))
What's going on here? How do I fix it?
Also, how can I get the x axis labels to all be right justified on the same line?
First - what you present isn't a reproducible example and nobody wants to sign up to access your data to help you out...
In your code:
states = healthd[[1]]
and
insuredChange = healthd[[4]]
are assigning the columns to the global environment - they are not changing the name of the values in your data.frame. When you use ggplot it is looking for columns in your data.frame with the names that don't exist - hence the NULL statement
healthd$states = healthd[[1]]
healthd$insuredChange = healthd[[4]]
will change it to something that should work - though I don't have the data so am not completely sure.
This should now generate the figure you want.
ggplot(healthd, aes(states, insuredChange)) +
geom_bar(stat="identity") +
theme(axis.text.x=element_text(angle = 90, hjust = 1))

No line in plot chart despite + geom_line()

I've read documentation and I think that my code should be right, but still there is no line between the points in the output. What is wrong?
The x'axis is discrete and y'axis is continuous.
My code
point.sqrmPrice <- ggplot(overview.df, aes(x = areaSize, y = sqrmPrice)) +
geom_line() +
geom_point() +
scale_y_continuous(breaks = c(seq(min(overview.df$sqrmPrice), max(overview.df$sqrmPrice), by = 10000) )) +
theme_bw()
The underlying issue here is a duplicate of this stack post.
Here's a reproducible example showing what #SN248 meant about adding group to the code
ggplot(iris, aes(x = factor(Sepal.Length), y = Sepal.Width)) +
geom_line(aes(group=1)) + geom_point() + theme_bw()
You are not getting a line because areaSize is a factor. Convert to numeric with
overview.df$areaSize <- as.numeric(as.character(overview.df$areaSize))
and then make the plot.
What you have to think about it is, do you expect a single line to connect all the dots?
Else, how many lines do you expect, that will tell you how many groups will you need to have.
You are missing the group aesthetic required for geom_line(), because you haven't specified how many groups (lines) you want in your plot.

Why this command in ggplot is returning an error?

I'm learning ggplot2 and I don't understand why this doesn't work :
p <- ggplot(diamonds, aes(x = carat))
p <- p + layer(
geom = "point",
stat = "identity"
)
p
Error in as.environment(where) : 'where' is missing
Do you know why?
I think the problem is that you haven't specified what to use for the y-values. ggplot2 doesn't have the same default as the base graphics for plotting points against their index values. To use geom_point() with stat="identity" you'd need something like:
p<-ggplot(diamonds, aes(x=carat, y=cut))
p+layer(geom="point", stat="identity")
or more commonly
p+geom_point(stat="identity")
or however else you want try to plot your data.
Generally you don't use layer to build up a plot. Instead, you use geom or stat. p + geom_point() will plot what you're looking for. I'd suggest working through some of the examples in the gplot2 documentation.

Resources