如何在R中使用ggplot绘制基于Usage的Asset Name-Time时间序列图?
Solution for Visualizing Asset Name vs Time by Usage Category
Looks like you're trying to create a clear plot that maps asset events (grouped by their Usage type) across time—let's get this working smoothly with ggplot2.
Step 1: Prepare Your Dataset
First, let's convert your raw table into a usable R data frame and fix the time format (since your Time values are currently text, ggplot won't sort them correctly otherwise):
# Load required libraries library(ggplot2) library(hms) # For handling time-only values # Recreate your dataset as a structured data frame DTPlot <- data.frame( Asset.Name = c("A", "A", "B", "C", "A", "B", "A", "C", "B", "A", "B", "B", "A"), Time = c("10:39:49", "10:46:35", "11:06:55", "11:24:05", "11:31:35", "13:46:17", "10:35:43", "10:40:55", "10:45:05", "11:31:35", "10:59:44", "11:44:36", "11:49:22"), Usage = c("CPU usage", "CPU usage", "CPU usage", "Memory usage", "Memory usage", "CPU usage", "Virtual Machine Failover Failed", "CPU usage", "CPU usage", "CPU usage", "Memory usage", "CPU usage", "Memory usage") ) # Convert Time column to a proper time format (so ggplot can sort and display it correctly) DTPlot$Time <- as_hms(DTPlot$Time)
Step 2: Complete Your ggplot Code
A quick note on your original code: you don't need to use DTPlot$ inside the aes() call—ggplot automatically pulls columns from the data frame you pass to ggplot(). Here's the polished, functional code:
# Build the final plot ggplot(DTPlot, aes(x = Time, y = Asset.Name, color = Usage)) + geom_jitter(width = 0, height = 0.1, size = 3) + # Small vertical jitter to avoid overlapping points labs( title = "Asset Events by Usage Type", x = "Time of Day", y = "Asset Name", color = "Event Category" ) + theme_minimal() + theme( plot.title = element_text(hjust = 0.5, size = 14, face = "bold"), axis.text.x = element_text(angle = 45, hjust = 1) # Rotate time labels for readability )
Key Details Explained
as_hms(Time)converts your text-based time values into a format that ggplot can sort chronologically (critical for a logical x-axis).geom_jitter()adds a tiny vertical offset to points that would overlap (e.g., Asset A has two events at 11:31:35) so you can see every event clearly. If you prefer not to jitter, usegeom_point(position = position_dodge(height = 0.2))to separate overlapping points by theirUsagecategory instead.- The theme and label tweaks make the plot clean and easy to interpret at a glance.
内容的提问来源于stack exchange,提问作者HJain
相关产品推荐
相关产品推荐

