首先,这是我的数据:
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
我想要列出Reduction
列中的数字,而yaxis
应该使用列名称。两个轴都应该从0到1200缩放。
现在是棘手的部分。我想在图表中添加您可以找到数字1的行中的值。如果每行中有多个1,则图表上应该有多个点。
答案 0 :(得分:4)
我真的很喜欢使用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))
答案 1 :(得分:1)
我认为以下应该解决你的问题,
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))