简单的点图,2种颜色,2列数据

时间:2016-04-20 13:40:02

标签: r plot ggplot2

我在带有df = as.data.frame(read.table("file.txt"))

的文本文件中导入了以下数据
    AED round2 round3
1  0.00  0.020  0.022
2  0.02  0.041  0.045
3  0.04  0.066  0.073
4  0.06  0.094  0.103
5  0.08  0.120  0.132
6  0.10  0.146  0.160
7  0.12  0.171  0.189
8  0.14  0.195  0.215
9  0.16  0.218  0.241
10 0.18  0.240  0.265

现在我想制作一个简单的点图,其中y轴上的round2值与x轴上的AED值相同,并且在同一图表中,第二个图表显示了round3的值,其中颜色不同,刻度为0.10区间

我提出的最佳解决方案是qplot(data=df, AED, round2, color="Round2")

但我需要一些帮助,如何在那里得到第二个图,以及如何将轴上的间距从0.25改为0.10

我在这里阅读了教程http://www.cookbook-r.com/Graphs/Axes_%28ggplot2%29/,但他们使用的是不同的数据布局,并为每行显式指定了组,而不是简单的标题。

那么如何才能让每个列制作1个图? (在一张图中)

2 个答案:

答案 0 :(得分:2)

试试这个:

library(ggplot2)
library(tidyr)

# wide to long format
plotDat <- gather(df, Group, myValue, -1)

# plot
ggplot(plotDat, aes(AED, myValue, col = Group)) +
  geom_point() +
  #fix breaks on axis
  scale_x_continuous(breaks = seq(0, 1, 0.1)) +
  scale_y_continuous(breaks = seq(0, 1, 0.1))

enter image description here

答案 1 :(得分:0)

以下是使用基本图形dotplot with two variables

中的dotchart的解决方案
# Make data frame with a factor and two numeric ranges 
Dt <- data.frame(Item = c("a","b","c","d","e","f"), 
                 V1 = seq(1:6), 
                 V2 = seq(1:6)+.2
                 )

# Plot dotchart for first range
dotchart(Dt$V1,
         labels = Dt$Item, 
         pch = 20,
         pt.cex = 2 ,
         xlim = c(0, max(Dt$V2)),
         col = "blue")

# Allow over-plotting
par(new = TRUE)

# Plot dotchart for second range
dotchart(Dt$V2,
     labels = Dt$Item, 
     pch = 15, 
     pt.cex = 2, 
     xlim = c(0, max(Dt$V2)),
     col = "black"
     )

# Add a legend for each range
legend("bottomright", 
   c("V1","V2"),
   pch = c(20,15),
   col = c("blue","black") 
   )
相关问题