R 在美国地图上绘制疾病地图

R 在美国地图上绘制疾病地图,r,ggplot2,maps,R,Ggplot2,Maps,我正在使用ggplot2和maps在美国地图上绘制埃博拉疾病的有限数据集 参数为状态和埃博拉感染是/否 含有该病毒的国家如下: Texas Yes Newyork Yes 这些州用红色表示,该国其他州用绿色表示 我不知道如何编码这个,任何帮助都将不胜感激 下面是我可以使用stackoverflow上的其他线程构建的代码 library(ggplot2); library(maps); library(plyr); library(mapproj); ebolad = read.csv('/us

我正在使用ggplot2和maps在美国地图上绘制埃博拉疾病的有限数据集

参数为状态和埃博拉感染是/否

含有该病毒的国家如下:

Texas Yes
Newyork Yes
这些州用红色表示,该国其他州用绿色表示

我不知道如何编码这个,任何帮助都将不胜感激

下面是我可以使用stackoverflow上的其他线程构建的代码

library(ggplot2);
library(maps);
library(plyr);
library(mapproj);
ebolad = read.csv('/usa.csv');
#a data set including the state name and whether it is effected or not (yes/no)
colClasses = c('character', 'character', 'numeric')[[2]];
names(ebolad) = c('col', 'region', 'infected');
ebolad$region  = tolower(ebolad$region);
us_state_map = map_data('state');
map_data = merge(ebolad, us_state_map, by = 'region'); 
map_data = arrange(map_data, order);
ggplot(map_data, aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill=infected)) +
  geom_path(colour = 'gray', linestyle = 2) +
  scale_fill_brewer('States affected with EBOLA Virus in USA', palette = 'PuRd') +
  coord_map();

有人能帮我改进绘图吗?

试着用手动刻度代替填充。这是取代秤、灌装机和啤酒厂。。。借

然而,这并没有给出最美丽的绿色和红色。但您可以使用十六进制代码定义任意颜色,例如值=c4daf4a、e41a1c

哪个值为红色可能取决于数据的详细信息。如果未感染状态应为绿色,只需使用value=cred,green切换颜色即可

如果您的问题与文件usa.csv有关,则如果没有该文件,则很难提供帮助。我通过以下命令生成数据:

ebolad<-data.frame(region=state.name,infected="no",stringsAsFactors=FALSE)
ebolad[ebolad$region %in% c("Texas","New York"),"infected"] <- "yes"

然后,使用前面提到的修改代码,我得到了一个不错的绘图

谢谢你花时间回答我的问题。这里是指向文件usa.csv的链接:我使用了您的代码来读取该文件,它对我有效。根据存储usa.csv的位置和工作目录,您可能必须从文件名中删除/。您是否能够使用“比例填充”手册生成绘图?如果没有,您是否收到任何错误消息?是的,我使用了以下命令,绘图看起来比“美国受埃博拉病毒影响的州”的比例要好得多,数值=c4daf4a,e41a1c非常感谢您的输入@stibu如何在地图上标记州名?我正在尝试命名受感染的州并保留REST。我使用了下面的“代码”,并且能够生成一个包含所有州名缩写的地图。可以在此处查看州地图[link]您的问题和答案不可复制,您在评论中链接到的图在未获得访问权限的情况下不可用。从长远来看,考虑这一点的净价值。
  #Code to map USA states affected with Ebola Virus
    #import the following libraries
    library(ggplot2);
    library(maps);
    library(plyr);
    #begin of code
    # read the csv file containing the ebola data for usa (important: replace the directory path)
    ebolad = read.csv('usa.csv');
    colClasses = c('character', 'character', 'numeric')[[2]];
    names(ebolad) = c('col', 'region', 'infected');
    ebolad$region  = tolower(ebolad$region);
    # import the usa state data into local dataset
    us_state_map = map_data('state');
    # merge ebola data set and usa maps data set
    map_data = merge(ebolad, us_state_map, by = 'region'); 
    map_data = arrange(map_data, order);
    # storing the data of abbreviated state names to display on the final map
    states <- data.frame(state.center, state.abb)
    # code to plot the map with state names and colors to distinguish the infected states vs uninfected states
    ggplot(map_data, aes(x = long, y = lat, group = group)) +
    geom_polygon(aes(fill=infected)) +
    geom_path(colour = 'gray', linestyle = 2) +
    xlab("Longitude") + ylab("Latitude") +
    geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 4)+
    scale_fill_manual('States affected with EBOLA Virus in USA', values=c("green4","red3")) +
    coord_map(project="globular") +
    theme_grey();
    #end of code
  #Code to map USA states affected with Ebola Virus
    #import the following libraries
    library(ggplot2);
    library(maps);
    library(plyr);
    #begin of code
    # read the csv file containing the ebola data for usa (important: replace the directory path)
    ebolad = read.csv('usa.csv');
    colClasses = c('character', 'character', 'numeric')[[2]];
    names(ebolad) = c('col', 'region', 'infected');
    ebolad$region  = tolower(ebolad$region);
    # import the usa state data into local dataset
    us_state_map = map_data('state');
    # merge ebola data set and usa maps data set
    map_data = merge(ebolad, us_state_map, by = 'region'); 
    map_data = arrange(map_data, order);
    # storing the data of abbreviated state names to display on the final map
    states <- data.frame(state.center, state.abb)
    # code to plot the map with state names and colors to distinguish the infected states vs uninfected states
    ggplot(map_data, aes(x = long, y = lat, group = group)) +
    geom_polygon(aes(fill=infected)) +
    geom_path(colour = 'gray', linestyle = 2) +
    xlab("Longitude") + ylab("Latitude") +
    geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 4)+
    scale_fill_manual('States affected with EBOLA Virus in USA', values=c("green4","red3")) +
    coord_map(project="globular") +
    theme_grey();
    #end of code