R 复杂点图-每行有多个点
首先是我的数据:R 复杂点图-每行有多个点,r,R,首先是我的数据: structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 3.9,
6.4, 7.4, 8.1, 9, 9.4, 7.8, 12.8, 14.8, 16.2, 18, 18.8), .Dim = c(6L,
22L), .Dimnames = list(c("Merc", "Peug", "Fera", "Fiat", "Opel",
"Volv"), c("10", "33.95", "58.66",
"84.42", "110.21", "134.16", "164.69", "199.1", "234.35", "257.19",
"361.84", "432.74", "506.34", "581.46", "651.71", "732.59", "817.56",
"896.24", "971.77", "1038.91", "Reduction", "Price")))
为了更容易地解释我想要实现的目标,我将展示矩阵:
> head(data)
10 33.95 58.66 84.42 110.21 134.16 164.69 199.1 234.35 257.19 361.84 432.74 506.34 581.46 651.71 732.59 817.56 896.24 971.77 1038.91 Reduction Price
Merc 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 3.9 7.8
Peug 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 6.4 12.8
Fera 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 7.4 14.8
Fiat 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 8.1 16.2
Opel 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9.0 18.0
Volv 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 9.4 18.8
如您所见,所有行只有两种类型的数字(0和1)。有时,每行中可能有1个以上。在xaxis
上,我想输入reduce
列中的数字,对于yaxis
应使用列名。两个轴的比例应在0到1200之间
现在是棘手的部分。我想把你能找到数字1的行中的值放在图表上。如果每行中有多个1,则图形上应有多个点 我真的很喜欢用
dplyr
来处理这类事情,因为它可以让你的代码既紧凑又易于阅读,即使你从现在起三个月后就开始使用它
require(dplyr)
require(tidyr)
require(ggplot2)
d <- data %>%
as.data.frame %>%
mutate(Maker = rownames(data)) %>%
gather(Column, Bool, -Maker, -Reduction, -Price) %>%
filter(Bool == 1) %>%
mutate(Column = as.numeric(levels(Column))[Column]) # Is factor otherwise
ggplot(d, aes(x=Reduction, y=Column, shape=Maker)) +
geom_point() +
scale_x_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200)) +
scale_y_continuous(limits=c(0, 1200), breaks=c(0, 400, 800, 1200))
require(dplyr)
需要(三年)
需要(ggplot2)
d%
as.data.frame%>%
变异(Maker=行名(数据))%>%
聚集(列、布尔、制造商、降价、价格)%>%
过滤器(布尔==1)%>%
mutate(Column=as.numeric(levels(Column))[Column])#是其他因素
ggplot(d,aes(x=减少,y=列,形状=制造者))+
几何点()+
比例x连续(极限=c(0,1200),中断=c(0,400,800,1200))+
刻度连续(极限=c(0,1200),中断=c(0,400,800,1200))
我认为以下内容应该可以解决您的问题
df = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 3.9,
6.4, 7.4, 8.1, 9, 9.4, 7.8, 12.8, 14.8, 16.2, 18, 18.8),
.Dim = c(6L, 22L),
.Dimnames = list(c("Merc", "Peug", "Fera", "Fiat", "Opel",
"Volv"),
c("10", "33.95", "58.66",
"84.42", "110.21", "134.16", "164.69", "199.1", "234.35", "257.19",
"361.84", "432.74", "506.34", "581.46", "651.71", "732.59", "817.56",
"896.24", "971.77", "1038.91", "Reduction", "Price")))
df = as.data.frame(df)
df$Price = NULL
library(reshape)
meltDF = melt(df, id.vars = 'Reduction')
library(ggplot2)
ggplot(meltDF[meltDF$value == 1,]) + geom_point(aes(x = Reduction, y = variable))
这没关系,但通过缩放,我的意思是我希望看到y轴和x轴上从0到1200的值。我知道,在这种情况下,点可能不可见,因为它们都将完全位于左侧。我的数据比我展示的示例长得多,
xaxis的值也会更高。我想在两个轴上都看到像0400801200这样的数字。这就是我所说的缩放。好吧,我想我一定是误解了右边有这么多空格,但如果你有额外的数据,这是有意义的。这就是我想要实现的,但当我尝试对我的全部数据进行缩放时,它会给我一个错误错误:提供给连续缩放的离散值。我知道这是我的错,因为在给定的例子中,它工作得很完美,但也许你知道哪里出了问题?这很奇怪。如果reduce
或Column
是一个因子而不是一个数值,则会发生这种情况,例如,如果在数据帧中放置字符向量而不使用stringsAsFactors=FALSE
,则可能会发生这种情况。尝试aes(x=as.numeric(levels(reducement))[reduce],y=as.numeric(levels(Column))[Column])
--。