t R上的X轴不按顺序绘图 [英] t X axis on R doesn't plot in order

查看:109
本文介绍了t R上的X轴不按顺序绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这个数据框:

 名字xy 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,0 -0,01 1,1
11 A0,5 0 0,1 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

这是我的代码:

  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(\\\
(BV)0)+
ylab((UB)0 \\\
)+
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 as Estrelas do AglomeradoGaláticoNGC6025 \\\



问题是:
前10个数据点被绘制成10º点t是先画出来的,直到11º点,然后它回到正常状态,画出12º,13º。

我试图按照它的顺序绘图在数据框中,我正在寻找一种方法来使Y图中的负值显示在顶部,而正数则显示在底部。

你能帮忙吗?请问我的问题是什么?

解决方案

看起来问题在于你的数据不是 numeric 。由于逗号,它被读入为字符,然后转换为因子



您可以将变量替换为数字,只需将逗号替换为句点,然后转换为数字或使用 dec 参数 read.table 来声明逗号用于小数点。



一个例子,对你的数据

  test<  -  tempfile()

writeLines(names xy 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,07 1,7
13 A4 0,1 0,08 1,9
1 4 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)

只要将小数点指定为逗号即可

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

或转换为数字(参见如何重新广告数据时,一些数字包含逗号作为千位分隔符?))

  df2 < -  read.table(test, sepp =)

df2 [2:4]< - lapply(df2 [2:4],函数(X)as.numeric(gsub(,,。,X )))


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?

解决方案

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)))

这篇关于t R上的X轴不按顺序绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆