R ggplot - 包括y轴的下限和上限

时间:2014-07-23 14:56:15

标签: r ggplot2

我在R中使用ggplot将我的数据表示到一个图中。问题是y轴(0和1.45)的下限和上限不包括在内

enter image description here

data <- data.frame(User= numeric(0), Time= numeric(0), 
                   Category= character(), stringsAsFactors=FALSE)

data[1,] <- c(1,0.42,"Category 1")
data[2,] <- c(2,0.63,"Category 1")
data[3,] <- c(3,0.50,"Category 1")
data[4,] <- c(4,0.72,"Category 1")
data[5,] <- c(4,0.73,"Category 2")
data[6,] <- c(5,0.60,"Category 1")

library(ggplot2)

ggplot(data= data, aes(x=User, y=Time, fill= Category)) + 
geom_bar(stat="identity")

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

问题在于您制作数据框的方式。当您输入c()个数字和字符时,会对字符进行编号。您可以在数据框中看到使用str()

str(data)
'data.frame':   6 obs. of  3 variables:
 $ User    : chr  "1" "2" "3" "4" ...
 $ Time    : chr  "0.42" "0.63" "0.5" "0.72" ...
 $ Category: chr  "Category 1" "Category 1" "Category 1" "Category 1" ...

直接制作数据框将提供核心格式和核心图。

data<-data.frame(
      User=c(1,2,3,4,4,5),
      Time=c(0.42,0.63,0.5,0.72,0.73,0.6),
      Category=c("Category 1","Category 1","Category 1",
                "Category 1","Category 2","Category 1"))
str(data)
'data.frame':   6 obs. of  3 variables:
 $ User    : num  1 2 3 4 4 5
 $ Time    : num  0.42 0.63 0.5 0.72 0.73 0.6
 $ Category: Factor w/ 2 levels "Category 1","Category 2": 1 1 1 1 2 1