如何以正确的方式使x轴顺序

时间:2019-02-07 19:49:02

标签: r ggplot2

有时候我一直在努力谋划

df<- structure(list(x = c("1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12", "1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12"), variable = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L), .Label = c("my_rep1a", "you_rep2a"), class = "factor"), 
    value = c(-7.70891, 0.9699727, 3.644688, 2.810303, -4.579114, 
    -6.57653, -1.938455, 3.201102, 4.951608, -1.263285, -0.9699727, 
    3.675765, -2.269753, -1.255846, 1.336035, -0.7997434, -0.4488655, 
    0.4488655, 0.7199451, 2.504063, 0.7398947, 0.6827841, -3.841076, 
    -3.018841)), row.names = c(NA, -24L), class = "data.frame")

所以我这样绘制

ggplot(data = df, aes(x = x, y = value,
          group = variable, color = variable)) + geom_line()

它在x轴上的顺序不正确

我确实尝试将x更改为df$x <- as.character(df$x)字符,但没有帮助。将其转换为整数

时相同

如果我使用整数或数字,它将在x轴上给我不同的数字,我不知道它是否正确绘制了它

enter image description here

1 个答案:

答案 0 :(得分:0)

一种方法是熟悉软件包function chooseCard () { let hand = []; for(const card of cards) { const addCard = function () {hand.push(card.id)}; card.addEventListener('click', addCard()); let id1 = hand[0]; let id2 = hand[1]; console.log(hand); if(hand[0] === hand[1]) { addPoint(); } else { changePlayer(); if(card.id == id1 || card.id == id2) { card.firstElementChild.src = `img/cardback.jpeg`; } hand = []; } } } 和函数stringr。设置参数str_sort以对字符进行数字排序。

numeric = TRUE

enter image description here