Run the following code in your R console. states <- data.frame(state.region, state.x77) Draw a Murder and non−negligent manslaughter rate per 100,000 population in Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware Florida Georgia Hawaii Idaho Illinois Indiana Iowa Kansas Kentucky Louisiana Maine Maryland Massachusetts Michigan Minnesota Mississippi Missouri Montana Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina North Dakota Ohio Oklahoma Oregon Pennsylvania Rhode Island South Carolina South Dakota Tennessee Texas Utah Vermont Virginia Washington West Virginia Wisconsin Wyoming The horizontal axis is Murder and non−negligent manslaughter rate and the vertical axis is Northeast:vemont rhode island Pennsylvania new York new jersey new Hampshire Massachusetts Maine Connecticut South: West Virginia Virginia Texas Tennessee South Carolina Oklahoma North Carolina Mississippi Maryland Louisiana Kentucky Georgia Florida Delaware Arkansas Alabama North Central: Wisconsin South Dakota Ohio North Dakota Nebraska Missouri Minnesota Michigan Kansas Iowa Indiana Illinois West: Wyoming Washington Utah Oregon New Mexico Nevada Montana Idaho Hawaii Colorado California Arizona Alaska And classify these states according to Northeast South central west on the vertical axis. Divide them into four parts in one picture
时间: 2023-06-27 22:04:09 浏览: 115
To draw a scatterplot with data points classified by region, we need to first add a region column to the `states` dataframe.
```
# Add region column to states dataframe
states$region <- ifelse(states$state.region == "Northeast", "Northeast",
ifelse(states$state.region %in% c("South", "South Atlantic", "East South Central", "West South Central"), "South Central",
ifelse(states$state.region %in% c("West", "Mountain", "Pacific"), "West", "North Central")))
```
Then, we can use the `ggplot2` package to create a scatterplot with points color-coded by region.
```
# Load ggplot2 package
library(ggplot2)
# Create scatterplot with points colored by region
ggplot(states, aes(x = Murder, y = state.x77.Population)) +
geom_point(aes(color = region)) +
scale_color_manual(values = c("Northeast" = "blue", "South Central" = "green", "West" = "red", "North Central" = "purple")) +
labs(title = "Murder and Non-Negligent Manslaughter Rate vs Population",
x = "Murder and Non-Negligent Manslaughter Rate (per 100,000 population)",
y = "Population")
```
This should produce a scatterplot with points colored by region, divided into four quadrants.
Note that some states may not be classified into the exact region they are in due to the way we defined the regions in the `ifelse` statement.
阅读全文