t X axis on R doesn't plot in order

2019-07-26 17:43发布

问题:

Using this dataframe:

names   x   y   MV

1   O4  -0,33   -1,2    -5,2
2   O9,5 -0,305 -1,1    -3,6
3   B0  -0,3    -1,08   -3,25
4   B0,5 -0,28  -1  -2,6
5   B1,5    -0,25   -0,9    -2,1
6   B2,5    -0,22   -0,8    -1,5
7   B3  -0,2    -0,69   -1,1
8   B6  -0,15   -0,5    -0,2
9   B8  -0,1    -0,3    0,6
10  B9,5    -0,05   -0,1    1,1
11  A0,5    0   0,01    1,5
12  A2  0,05    0,05    1,7
13  A4  0,1 0,08    1,9
14  A5  0,15    0,09    2,1
15  A7  0,2 0,1 2,4
16  A8  0,25    0,07    2,55
17  F0  0,3 0,03    2,8
18  F2  0,35    0   3,1
19  F4  0,4 -0,01   3,4
20  F8  0,5 0   4,1
21  G0  0,6 0,08    4,7
22  G6  0,7 0,23    5,2
23  K0  0,8 0,42    5,8
24  K2  0,9 0,63    6,3
25  K3,5    1   0,86    6,7
26  K4,5    1,1 1,03    7,1
27  K5,5    1,2 1,13    7,5
28  K6,5    1,3 1,2 8
29  M0  1,4 1,22    8,8

Here is my Code:

df <- read.table(file = "Pasta1.txt", sep = "\t", header = T)
star <- ggplot(data=df, aes(x=x, y=y, group=1)) +
  geom_point(alpha=1, size=5) +
  geom_text(aes(label=names),hjust=-0.4, vjust=0.8) +
  xlab("\n (B-V)0") +
  ylab("(U-B)0 \n") +
theme_bw() +
  theme( axis.title.y = element_text(face="bold", size=16),
    axis.title.x = element_text(face="bold", size=16),
    axis.text = element_text(size=12),
    legend.title =element_blank() ,
    legend.text = element_text(size = 12)) +
  theme(strip.text = element_text(size=12, face="bold"))
star + ggtitle("Diagrama Cor - Cor para Estrelas do Aglomerado Galático NGC6025 \n")

The problem is that : The first 10 data points are plotted inverted the 10º point is ploted first and so on until the 11º point and then it came back to normal, ploting the 12º, 13º.

I am trying to plot in the order it is on the dataframe and I am seeking a method to make the negative values in the Y graph be shown on the top, and the positive numbers on the bottom.

Could you help me please?

回答1:

It look like the problem is that your data is not numeric. Due to the comma, it is being read in as character and then converted to a factor.

You can change the variables to numeric by substituting out the comma for a period, and then converting to numeric, or by using the dec argument of read.table to declare that the comma is used for the decimal point.

An example, on your data

test <- tempfile()

writeLines("names   x   y   MV
1   O4  -0,33   -1,2    -5,2
            2   O9,5 -0,305 -1,1    -3,6
            3   B0  -0,3    -1,08   -3,25c
            4   B0,5 -0,28  -1  -2,6
            5   B1,5    -0,25   -0,9    -2,1
            6   B2,5    -0,22   -0,8    -1,5
            7   B3  -0,2    -0,69   -1,1
            8   B6  -0,15   -0,5    -0,2
            9   B8  -0,1    -0,3    0,6
            10  B9,5    -0,05   -0,1    1,1
            11  A0,5    0   0,01    1,5
            12  A2  0,05    0,05    1,7
            13  A4  0,1 0,08    1,9
            14  A5  0,15    0,09    2,1
            15  A7  0,2 0,1 2,4
            16  A8  0,25    0,07    2,55
            17  F0  0,3 0,03    2,8
            18  F2  0,35    0   3,1
            19  F4  0,4 -0,01   3,4
            20  F8  0,5 0   4,1
            21  G0  0,6 0,08    4,7
            22  G6  0,7 0,23    5,2
            23  K0  0,8 0,42    5,8
            24  K2  0,9 0,63    6,3
            25  K3,5    1   0,86    6,7
            26  K4,5    1,1 1,03    7,1
            27  K5,5    1,2 1,13    7,5
            28  K6,5    1,3 1,2 8
            29  M0  1,4 1,22    8,8", test)

Just read in specifying the decimal point as a comma

df <- read.table(test, sep="", dec=",")

Or convert to numeric (see How to read data when some numbers contain commas as thousand separator?)

df2 <- read.table(test, sep="")

df2[2:4] <- lapply(df2[2:4], function(X) as.numeric(gsub(",",".", X)))


标签: r plot ggplot2