You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言多条件判断列转行及绘制李克特图的实现咨询

数据处理与Likert图绘制实现方案

1 数据预处理(Pandas实现)

用Pandas即可完成宽表转长表的需求,参考代码如下:

import pandas as pd

# 此处替换为你的原始数据表赋值
df = pd.read_csv("你的原始数据路径.csv") 

# 第一步:筛选出action/fun/history至少有一个字段值大于3的记录
filter_mask = (df['action'] > 3) | (df['fun'] > 3) | (df['history'] > 3)
df_filtered = df[filter_mask].copy()

# 第二步:宽表转长表,生成type字段
df_melted = df_filtered.melt(
    id_vars=['id', 'usefulness', 'intention'],
    value_vars=['action', 'fun', 'history'],
    var_name='type',
    value_name='original_score'
)
# 只保留原字段值大于3的行,得到最终处理后的数据
df_final = df_melted[df_melted['original_score'] > 3].reset_index(drop=True)

处理后的数据包含id、usefulness、intention、type、original_score五个字段,完全匹配需求。

2 Likert图绘制

可以用plotly.express快速实现要求的分组Likert图,参考代码如下:

import plotly.express as px

# 转换数据格式适配双分组需求
df_plot = df_final.melt(
    id_vars=['type'],
    value_vars=['usefulness', 'intention'],
    var_name='group_dimension',
    value_name='likert_score'
)

# 绘制横向堆叠Likert图
fig = px.bar(
    df_plot,
    y='type',
    color='likert_score',
    facet_col='group_dimension',
    orientation='h',
    barmode='relative',
    title='各类型下有用性与用户意向的Likert分布'
)
fig.show()

如果偏好Matplotlib实现,引入第三方likert绘图库后,将分类字段指定为type、分组字段指定为usefulness/intention即可完成绘制。

内容的提问来源于stack exchange,提问作者Jonas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 13:57:04