我正在尝试创建一个点图,其中国家在我的Y轴上从A-Z从上到下列出。奖牌计数将是四个地块中每个地块的X轴,每个地块分别为金,银,铜和总数。当然,ggplot喜欢用Z-A绘制国家,尽管已经阅读了所有关于这个问题的内容,但我还是没有解决这个问题。我很感激在编码和理解方面提供任何直接的帮助。
mdat <- melt(raw, value.name = "Count", variable.name = "Place", id.var = "Country")
mdat[, "Place"] <- factor(mdat[, "Place"], levels=c("Gold", "Silver", "Bronze", "Total"))
##I know my problem is likely on or around the above line ##
plot1 <- ggplot(mdat, aes(x = Count, y = Country, colour = Place)) +
geom_point() +
facet_grid(.~Place) + theme_bw()+
scale_colour_manual(values=c("#FFCC33", "#999999", "#CC6600", "#000000"))
print(plot1)
Algeria Gold 4
Argentina Gold 5
Armenia Gold 1
Algeria Silver 2
Argentina Silver 5
Armenia Silver 2
Algeria Bronze 4
Argentina Bronze 2
Armenia Bronze 0
答案 0 :(得分:1)
在绘制之前,您必须对Country
的级别进行排序。此外,您提供的数据没有Total
级别。以下appraoch应该给你想要的结果:
读取数据(包括Total
变量的Place
级别:
mdat <- read.table(text="Country Place Count
Algeria Gold 4
Argentina Gold 5
Armenia Gold 1
Algeria Silver 2
Argentina Silver 5
Armenia Silver 2
Algeria Bronze 4
Argentina Bronze 2
Armenia Bronze 0
Algeria Total 10
Argentina Total 12
Armenia Total 3", header=TRUE)
对Country
变量的级别进行排序:
mdat$Country <- factor(mdat$Country,levels=sort(unique(mdat$Country),decreasing=TRUE))
以正确的顺序获取Place
变量:
levels(mdat$Place) <- c("Bronze"=3,"Gold"=1,"Silver"=2,"Total"=4)
mdat$Place <- as.numeric(mdat$Place)
mdat$Place <- as.factor(mdat$Place)
levels(mdat$Place) <- c("Gold","Silver","Bronze","Total")
创建情节:
ggplot(mdat, aes(x = Count, y = Country, colour = Place)) +
geom_point(size=4) +
facet_grid(.~Place) + theme_bw()+
scale_colour_manual(values=c("#FFCC33","#999999","#CC6600","#000000"))
给出了以下情节:
当您已经融化数据时,我怀疑Total
数据框中没有raw
变量。你可以用以下方法计算:
raw$Total <- rowSums(..specify the Gold, Silver & Bronze columns here..)