考虑以下示例:
library(ggplot2)
input_data <- as.matrix(read.table("SpectData.txt"))
colnames(input_data) <- c("x1", "x2")
#1- Define Weights matrix
W <- matrix(0,nrow = nrow(input_data),ncol = nrow(input_data))
#2- Define Degree Matrix
D <- matrix(0,nrow = nrow(input_data),ncol = nrow(input_data))
calculateWeight <- function(x1,x2,sigma) {
result <- exp(- (norm(x2-x1,type = "2"))^2/ (2*sigma^2))
result
}
calcWieghtMatrix <- function(sigma) {
for(i in 1: nrow(W)){
for(j in 1: nrow(W)){
if(i == j){
next
}
if( W[i,j] != 0){
next
}
W[i,j] <<- calculateWeight(input_data[i,],input_data[j,],sigma)
W[j,i] <<- W[i,j]
}
}
}
calcDegreeMatrix <- function() {
for( i in 1:nrow(input_data)){
D[i,i] <<- sum(W[i,])
}
}
executeSpectralClustring <- function (sigma,numberOfClusters){
calcWieghtMatrix(sigma)
calcDegreeMatrix()
L <<- D - W
eigenDecompostion <- eigen(L,symmetric = FALSE)
index <- ncol(eigenDecompostion$vectors)-1
eigenVector <- eigenDecompostion$vectors[,index]
cl <- kmeans(eigenVector,numberOfClusters)
ggplot(input_data,col = cl$cluster)
}
executeSpectralClustring(0.01,2)
如上所示,x轴上的library(ggplot2)
library(dplyr)
set.seed(30)
data <- data.frame(group = factor(1:11),
year = c(rep("2013-2014", times = 11),
rep("2014-2015", times = 11),
rep("2015-2016", times = 11),
rep("2016-2017", times = 11)),
value = runif(44),
stringsAsFactors = FALSE)
data$plot_year <- as.Date(paste0("01/01/", substr(data$year, start = 1, stop = 4)),
format = "%m/%d/%Y")
ggplot(data, aes(x = plot_year, y = value, color = group)) +
geom_point() +
geom_line(linetype = "dotted") +
geom_line(data= data %>%
filter(as.numeric(substr(plot_year, start = 1, stop = 4)) < 2015),
aes(x = plot_year, y = value, color = group)) +
theme_bw()
与2013
相对应,2013-2014
与2014
相对应,依此类推。
如何使用这些轴标签 - 即2014-2015
,2013-2014
等 - 代替当前的x轴标签?我在网上找到的每个解决方案都说过以某种形式或形式使用2014-2015
,但这些都是学年,而不是固定日期。
答案 0 :(得分:3)
您可以直接使用学年作为绘图的x值。只要<=
是一个字符或有序因子,您就可以使用比较运算符(如year
)进行子集化(但如果year
是非有序因子则不行)。作为字符变量,排序将按字母顺序排列。我更喜欢有序因子,以便我可以指定顺序:
data$year = factor(data$year, levels=sort(unique(data$year)), ordered=TRUE)
ggplot(data, aes(x = year, y = value, color = group, group=group)) +
geom_point() +
geom_line(linetype = "dotted") +
geom_line(data= data %>% filter(year <= "2014-2015")) +
theme_bw()
虽然我更喜欢使用year
的顺序进行子集化,但您也可以明确指定要包含的年份:
ggplot(data, aes(x = year, y = value, color = group, group=group)) +
geom_point() +
geom_line(linetype = "dotted") +
geom_line(data= data %>% filter(year %in% c("2013-2014","2014-2015"))) +
theme_bw()
答案 1 :(得分:1)
您可以将日期转换为数字,然后将scale_x_continuous
与中断和标签参数一起使用:
library(ggplot2)
library(lubridate)
# calculate the breaks as numeric corresponding to the dates
br <- as.numeric(as.Date(c("2013-01-01", "2014-01-01", "2015-01-01", "2016-01-01")))
# calculate the labels at each break
lb <- c("2013-2014", "2014-2015", "2015-2016", "2016-2017")
ggplot(data, aes(x = as.numeric(plot_year), y = value, color = group)) +
geom_point() +
geom_line(linetype = "dotted") +
geom_line(data= data %>% filter(year(plot_year) < 2015),
aes(x = as.numeric(plot_year), y = value, color = group)) +
theme_bw() +
scale_x_continuous(breaks = br, labels = lb) + xlab("year")